Create a SQLite database with Python

In this example we create a SQLite database and insert some sample data into it, we will use this database in later examples

[codesyntax lang=”python”]

import sqlite3 as lite
import sys

con = lite.connect('test.db')

with con:
    
    cur = con.cursor()    
    cur.execute("CREATE TABLE Jobs(Id INT, Name TEXT, Salary INT)")
    cur.execute("INSERT INTO Jobs VALUES(1,'Programmer',30000)")
    cur.execute("INSERT INTO Jobs VALUES(2,'Manager',76000)")
    cur.execute("INSERT INTO Jobs VALUES(3,'Leader',45000)")
    cur.execute("INSERT INTO Jobs VALUES(4,'Admin',10000)")
    cur.execute("INSERT INTO Jobs VALUES(5,'IT',20000)")
    cur.execute("INSERT INTO Jobs VALUES(6,'SysAdmin',25000)")
    cur.execute("INSERT INTO Jobs VALUES(7,'CEO',90000)")
    cur.execute("INSERT INTO Jobs VALUES(8,'TeamLeader',35000)")

[/codesyntax]

Save the code above and run as normal, you wont see much but the db will be created

Related posts

TLV493D magnetic sensor and Raspberry Pi 4 python example

SHT40 Digital Humidity Sensor and Raspberry Pi 4 python example

LTR390 UV Light Sensor a Raspberry Pi 4 in python

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More