Example illustrating use of Python and sqlite
- Download and unzip sqlite. Downloading the sqlite tools zip file (named something like sqlite-tools-win32-x86-3310100.zip) worked for me.
- Download the sql script files create_univ_database.sql and smallRelationsInsertFile.sql
- Launch the sqlite3 command-line shell program
- 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
- .read create_univ_database.sql #similar to source filename in MySql
- .read smallRelationsInsertFile.sql
- .save university.db #creates a file corresponding to the database
- You can also submit sql queries at the shell prompt
- 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)