SQLite with Python
This tutorial will follow the same college.db
example in the SQLite presentation.
import sqlite3
db = 'college.db'
conn = sqlite3.connect(db)
c = conn.cursor()
We can now create the two tables:
- Programs with ID, program name, and description;
- Students with ID, name, program info, and class year.
using SQL statements with the SQLite3 interactive.
c.execute("drop table if exists programs")
c.execute("""create table programs (
program_id varchar(10) not null primary key,
program_name varchar(20),
program_desc varchar(500)
)""")
conn.commit()
Now we can start adding students data:
insert_sql = "insert into programs (program_id, program_name, program_desc) values (?, ?, ?)"
c.execute(insert_sql, ('IS', 'Information Systems', 'Information systems with a business angle') )
c.execute(insert_sql, ('DS', 'Data Science', 'New data science program') )
c.execute(insert_sql, ('CS', 'Computer Science', 'Traditional computer science') )
c.execute(insert_sql, ('CST', 'Computing & Security Technology', 'CST with a new focus on security') )
conn.commit()
Now let's take a look at data in the table:
c.execute("drop table if exists students")
c.execute("""
create table students (
student_id integer primary key,
student_name varchar(100),
program_id varchar(10),
class_year int
)
""")
conn.commit()
For the students tables, we have preppared a csv file with students records, which can be downloaded from: students.csv.
Suppose data file students.csv
is in the data sub-directory (folder):
csv
1 John Smith, IS, 2020
2 Albert Einstein, DS 2021
3 George Washington, CS, 2019
4 Donald Trump, CST, 2020
5 Barack Obama, IS, 2021
. ... .. ....
Let read the CSV file and load data into the students
table:
import csv
# remove all student data first
delete_sql = "delete from students"
c.execute(delete_sql)
# load CSV data
f=open("data/students.csv")
insert_sql = "insert into students values (?, ?, ?, ?)"
for row in csv.reader(f):
c.execute(insert_sql, row)
conn.commit()
Now check out the data:
c.execute("select * from students limit 10")
rows = c.fetchall()
for row in rows:
print(row)
Now let's count the number of records in the table:
c.execute("select count(*) from students")
rows = c.fetchall()
for row in rows:
print("The total number of students:", row[0])
query = """select program_id,
count(*) as student_count,
min(class_year) as first_class,
max(class_year) as last_class
from students
group by program_id
order by student_count DESC
"""
c.execute(query)
rows = c.fetchall()
for row in rows:
print(row[0], row[1], row[2], row[3])
update_sql = "update students set class_year = 2030 where class_year > 2030"
c.execute(update_sql)
conn.commit()
We can run the statistics again to see if data have been updated:
from pprint import pprint
query = """select program_id,
count(*) as student_count,
min(class_year) as first_class,
max(class_year) as last_class
from students
group by program_id
order by student_count DESC
"""
c.execute(query)
rows = c.fetchall()
pprint(rows)
query = """
select student_name, program_name, class_year
from students s join programs p
on s.program_id = p.program_id
limit 10
"""
c.execute(query)
rows = c.fetchall()
for row in rows:
print("{:s} {:s} {:d}".format(row[0].ljust(20), row[1].ljust(40), row[2]))
References
- Chapter 3 Managing Data, section Managing Data using SQL, of LauraCasselland AlanGauld (2014). Python Projects.https://ebookcentral-proquest-com.ezproxy2.library.drexel.edu/lib/drexel-ebooks/detail.action?docID=1875898
- SQLite operations in Python:http://sebastianraschka.com/Articles/2014_sqlite_in_python_tutorial.html