Example illustrating use of Python and sqlite

  1. Download and unzip sqlite. Downloading the sqlite tools zip file (named something like sqlite-tools-win32-x86-3310100.zip) worked for me.
  2. Download the sql script files create_univ_database.sql and  smallRelationsInsertFile.sql
  3. Launch the sqlite3 command-line shell program
  4. At the sqlite shell prompt, the following commands will read the sql script files referenced above and create the corresponding database file in the same folder
    1. .read create_univ_database.sql #similar to source filename in MySql
    2. .read smallRelationsInsertFile.sql
    3. .save university.db #creates a file corresponding to the database
    4. You can also submit sql queries at the shell prompt
  5. In addition, you can use sqlite (and corresponding databases) from a Python program. Launch a Python shell and follow the example below to execute SQL queries using the database. Obviously, this can also be done within a Python program.
import sqlite3
conn = sqlite3.connect('university.db')
c = conn.cursor()
results = c.execute("select * from instructor") #results is a list of tuples
for t in results: #iterate over the list and print each tuple
print(t)