I have posted homework 7 here.
It is due this Sunday at 11:59 PM.
I will start scoring the Midterm tomorrow.
I hope to be finished by Saturday.
There will be a graded quiz next week.
It will cover material from today's class and Class 13.
Today we will be discussing how to use Python to access a SQLite database.
I am doing this to show you how Python can be used with other sources of information.
In order to do this, I need to discuss some features of Structured Query Language (SQL).
This is not a database course, so you will not be required to remember this on the Final Exam.
import sys # opens a file for reading if it can # otherwise quits def open_file_read(filename): try: file = open(filename, "r") except: print("Cannot open", filename) sys.exit() else: return file # opens a file for writing def open_file_write(filename): try: file = open(filename, "w") except: print("Could not open", filename) sys.exit() else: return file
import utilities
file = utilities.open_file_read(filename)
from utilities import *I can call a function by simply using its name
file = open_file_read(filename)
$ sqlite3 SQLite version 3.16.0 2016-11-04 19:09:39 Enter ".help" for usage hints. Connected to a transient in-memory database. Use ".open FILENAME" to reopen on a persistent database. sqlite>
sqlite> select * from students; student_id first_name last_name umb_email user_name college ---------- ---------- ---------- --------------------- ---------- ---------- 09329034 Jane Adams Jane.Adams001@umb.edu jadams CSM 03687480 Alexander Smith Alexander.Smith001@um bigboy CM 05431692 Christophe Cannon Christopher.Cannon001 ccannon CSM 07511379 Joseph Malloney Joseph.Malloney001@um jmal CSM 07518979 Fatih Jones Fatih.Jones001@umb.ed fjones CM 04175276 James Reynolds James.Reynolds001@umb jr CSM
sqlite> select * from enrollments; id course_id section_no semester_id student_id ---------- ---------- ---------- ----------- ---------- 1 it116 1 s17 05431692 2 it116 1 s17 09329034 3 it116 1 s17 07518979 4 it244 1 s17 07511379 5 it244 1 s17 09329034 6 it244 1 s17 04175276 7 it116 1 sum17 03687480 8 it116 1 sum17 07511379 9 it116 1 sum17 07518979 10 it117 1 f18 07518979 11 it117 1 f18 05431692 12 it117 1 f18 09329034
create table students ( student_id text primary key unique, first_name text not null, last_name text not null, umb_email text not null unique, email text unique, user_name text unique, college text );
select COLUMN_LIST from TABLE_NAME where RESTRICTIONS;
sqlite> select student_id, first_name, last_name from students where college = "CM";
student_id first_name last_name
---------- ---------- ----------
03687480 Alexander Smith
07518979 Fatih Jones
sqlite> select * from enrollments where course_id = "it117" and semester_id = "f18";
id course_id section_no semester_id student_id
---------- ---------- ---------- ----------- ----------
10 it117 1 f18 07518979
11 it117 1 f18 05431692
12 it117 1 f18 09329034
sqlite> select enrollments.student_id, students.first_name, students.last_name, students.umb_email ...> from enrollments ...> inner join students ...> on enrollments.student_id = students.student_id ...> where course_id = "it117" and semester_id = "f18"; student_id first_name last_name umb_email ---------- ---------- ---------- ---------------------- 07518979 Fatih Jones Fatih.Jones001@umb.edu 05431692 Christophe Cannon Christopher.Cannon001@ 09329034 Jane Adams Jane.Adams001@umb.edu
create view students_enrollments_vw as select enrollments.student_id, students.first_name, students.last_name, students.umb_email, enrollments.course_id, enrollments.semester_id from enrollments inner join students on enrollments.student_id = students.student_id;
sqlite> select first_name, last_name, umb_email from students_enrollments_vw ...> where course_id = "it117" and semester_id = "f18"; first_name last_name umb_email ---------- ---------- ---------------------- Fatih Jones Fatih.Jones001@umb.edu Christophe Cannon Christopher.Cannon001@ Jane Adams Jane.Adams001@umb.edu
insert
command
insert into TABLE_NAME (COLUMN_LIST) values (VALUE_LIST);
create table enrollments ( id integer primary key, course_id text not null references courses(course_id), section_no integer not null, semester_id text not null references semesters(semester_id), student_id text not null references students(student_id), unique (student_id, course_id, semester_id) );
integer primary key
is specialsqlite> insert into enrollments values(null, "it244", 1, "f18", "05431692");
sqlite> select * from enrollments where course_id = "it244" and semester_id = "f18"; id course_id section_no semester_id student_id ---------- ---------- ---------- ----------- ---------- 13 it244 1 f18 05431692
update TABLE_NAME set(COLUMN_VALUE_PAIRS) where (CONDITION)
update students set user_name = "alex" where student_id = "03687480";
select * from students where student_id = "03687480"; student_id first_name last_name umb_email user_name college ---------- ---------- ---------- -------------------------- ---------- ---------- 03687480 Alexander Smith Alexander.Smith001@umb.edu alex CM
sqlite> update students set college = "CSM" where college != "CM"; sqlite> select * from students where college != "CM"; student_id first_name last_name umb_email user_name college ---------- ---------- ---------- --------------------- ---------- ---------- 09329034 Jane Adams Jane.Adams001@umb.edu jadams CSM 03687480 Alexander Smith Alexander.Smith001@um alex CSM 05431692 Christophe Cannon Christopher.Cannon001 ccannon CSM 07511379 Joseph Malloney Joseph.Malloney001@um jmal CSM 07518979 Fatih Jones Fatih.Jones001@umb.ed fjones CSM 04175276 James Reynolds James.Reynolds001@umb jr CSM
delete
command is used to remove a record from a table
delete from TABLE_NAME where CONDITION
sqlite> delete from enrollments where course_id = "it244" and semester_id = "f18";
>>> import sqlite3 >>> sqlite3 <module 'sqlite3' from '/usr/lib/python3.5/sqlite3/__init__.py'>
CONNECTION_OBJECT_VARIABLE = sqlite3.connect(DATABASE_FILENAME)
>>> import sqlite3 >>> con = sqlite3.connect("umb.db")
CURSOR_OBJECT_VARIABLE = CONNECTION_OBJECT_VARIALE.cursor()
>>> cur = con.cursor()
>>> import sqlite3 >>> con = sqlite3.connect("umb.db") >>> cur = con.cursor() >>> cur.execute("select * from students")
for
loop
>>> for row in cur: ... print(row) ... ('09329034', 'Jane', 'Adams', 'Jane.Adams001@umb.edu', 'jadams', 'CSM') ('03687480', 'Alexander', 'Smith', 'Alexander.Smith001@umb.edu', 'bigboy', 'CM') ('05431692', 'Christopher', 'Cannon', 'Christopher.Cannon001@umb.edu', 'ccannon', 'CSM') ('07511379', 'Joseph', 'Malloney', 'Joseph.Malloney001@umb.edu', 'jmal', 'CSM') ('07518979', 'Fatih', 'Jones', 'Fatih.Jones001@umb.edu', 'fjones', 'CM') ('04175276', 'James', 'Reynolds', 'James.Reynolds001@umb.edu', 'jr', 'CSM')
for
loopinsert
command
>>> cur.execute("insert into enrollments values(null, "it244", 1, "f18", "05431692")")
<sqlite3.Cursor object at 0x7febd141aab0>
unique (student_id, course_id, semester_id)
>>> cur.execute("insert into enrollments values(null, "it244", 1, "f18", "05431692")")
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
sqlite3.IntegrityError: UNIQUE constraint failed: enrollments.student_id, enrollments.course_id, enrollments.semester_id
insert
commandcon.commit()
update
command to
execute
>>> cur.execute("update students set user_name = "alex" where student_id = "03687480")
<sqlite3.Cursor object at 0x7f15373ffb20>
cur.execute("delete from enrollments where course_id = "it244", section_no = 1, semester = "f18", student_id = "05431692")
<sqlite3.Cursor object at 0x7f15373ffb20>>
insert into enrollments values (null, COURSE_ID, SECTION_NO, SEMESTER_ID, STUDENT_ID)
insert
statement
def enrollment_add(con, course_id, section_no, semester_id, student_id): stmnt = "insert into enrollments values(null, " + course_id + ", " + str(section_no) +\ ", " + semester_id + ", " + student_id + ")" cur = con.cursor() cur.execute(stmnt) cur.close()
insert into enrollments values(null, ?, ?, ?, ?)
def enrollment_add(con, course_id, section_no, semester_id, student_id):
cur = con.cursor()
cur.execute("insert into enrollments values(null, ?, ?, ?, ?)", (course_id, section_no, semester_id, student_id))
cur.close()