In these examples we are going to use SQLite as database.
To start to use SQLite with python we will need to import the library 'sqlite3', once imported we can start using it,
first we will establish a connection with the database using sqlite3.connect(), later, to start the navigation we will
need a cursor, for that we use .cursor().
There are two ways to execute SQL statements in python.
* executescript():allow me to execute several SQL statement art the same time, if this statement finish with ";".
* execute(): this will be limited to one SQL statement.
importsqlite3conn=sqlite3.connect('rosterdb.sqlite')cur=conn.cursor()cur.executescript('''DROP TABLE IF EXISTS User;DROP TABLE IF EXISTS Member;DROP TABLE IF EXISTS Course;''')
3. Now we will create 3 tables; "User", "Course", "Member". Member table contain a primary key composed of take two
parameters, this is a way to link to the other 2 tables and create the many-to-many relationship
importsqlite3conn=sqlite3.connect('rosterdb.sqlite')cur=conn.cursor()# Do some setupcur.executescript('''DROP TABLE IF EXISTS User;DROP TABLE IF EXISTS Member;DROP TABLE IF EXISTS Course;CREATE TABLE User ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT UNIQUE);CREATE TABLE Course ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, title TEXT UNIQUE);CREATE TABLE Member ( user_id INTEGER, course_id INTEGER, role INTEGER, PRIMARY KEY (user_id, course_id))''')
In previous steps we create the schema of the database, now we need to populate this database with information,
in this case we are going to extract information from a JSON file and use it to feed the dataset.
second we need to get the information of a JSON file, in this case we are going to use one of the JSON file examples
from the book "Python for everyone".
Now we are goin to insert the information in the different tables, for that we will use INSERT and we will add
IGNORE to avoid those cases when a error appears.
cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''',(name,))
in the previous statement execute() will execute an INSERT instruction to the "User" table, to the column "name",
the "?" is a placeholder and the "(name,)" is a tuple that indicate that the information on the variable "name" is
going to be place in the "?", and this information will be inserted in the table "User".
Bellow the example of the other statement, the only remark will be the usage of REPLACE which will replace the value
of what ever is in that column(s).
cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''',(name,))cur.execute('''INSERT OR IGNORE INTO Course (title) VALUES ( ? )''',(title,))cur.execute('''INSERT OR REPLACE INTO Member (user_id, course_id, role) VALUES ( ?, ?, ? )''',(user_id,course_id,role))
now we are going to select one of the records in the database and store the first row, for this we will execute the
SELECT and later use fetchone[0] to store the first record
Finally, to commit this changes or this addition to the database we can use the function commit(), this will commit
the changes to the database and wait until is done, that is one of the reason in some case the commit is done after
several changes and not after each change, since this will make the execution of the script slower.
importjsonimportsqlite3conn=sqlite3.connect('rosterdb.sqlite')cur=conn.cursor()# Do some setupcur.executescript('''DROP TABLE IF EXISTS User;DROP TABLE IF EXISTS Member;DROP TABLE IF EXISTS Course;CREATE TABLE User ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, name TEXT UNIQUE);CREATE TABLE Course ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE, title TEXT UNIQUE);CREATE TABLE Member ( user_id INTEGER, course_id INTEGER, role INTEGER, PRIMARY KEY (user_id, course_id))''')fname=input('Enter file name: ')iflen(fname)<1:fname='roster_data_sample.json'# [# [ "Charley", "si110", 1 ],# [ "Mea", "si110", 0 ],str_data=open(fname).read()json_data=json.loads(str_data)print(type(json_data))forentryinjson_data:name=entry[0]title=entry[1]role=entry[2]print((name,title,role))cur.execute('''INSERT OR IGNORE INTO User (name) VALUES ( ? )''',(name,))cur.execute('SELECT id FROM User WHERE name = ? ',(name,))user_id=cur.fetchone()[0]cur.execute('''INSERT OR IGNORE INTO Course (title) VALUES ( ? )''',(title,))cur.execute('SELECT id FROM Course WHERE title = ? ',(title,))course_id=cur.fetchone()[0]cur.execute('''INSERT OR REPLACE INTO Member (user_id, course_id, role) VALUES ( ?, ?, ? )''',(user_id,course_id,role))conn.commit()