Home Code Create a SQLite database with Python

Create a SQLite database with Python

by shedboy71

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

You may also like

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