Professional Documents
Culture Documents
2 Creating Tables
Lets write a function to read in a file and populate the table Precipitation that we saw earlier.
Parameters:
db: name/path to a database
# connect to database
# get cursor
1
# close cursor and connection
3 Accessing Data
How can we access the data in the database?
def print_all_precipitation(db):
"""(str) -> NoneType
Print all records in database db (name/path to a database) in
Precipitation table, one per line.
"""
conn = sqlite3.connect(db)
cur = conn.cursor()
cur.execute(select * from Precipitation)
for record in cur: print(cur.fetchall()) record = cur.fetchone()
print(record) while record is not None:
print(record)
record = cur.fetchone()
cur.close()
conn.close()
4 Simple Queries
Lets write the following functions to retrieve data from the table.
def city_snow(db):
"""(str) -> list of tuple
Return a list of tuples [(city, snow), ...] from the Precipitation
table in the database db (name/path to a database)."""
conn = sqlite3.connect(db)
cur = conn.cursor()
# execute query
# get all results
cur.close()
conn.close()
return result
def city_total(db):
"""(str) -> list of tuple
conn = sqlite3.connect(db)
cur = conn.cursor()
# execute query
# get all results
2
cur.close()
conn.close()
return result
Notice that the only line changing in the two functions is the one with cur.execute(). We can
take advantage of this by writing a general run query function!
def run_query(db, q):
"""(str, str) -> list of tuple
Return the result of executing the query q in database db
(name/path to a database)"""
conn = sqlite3.connect(db)
cur = conn.cursor()
cur.execute(q)
result = cur.fetchall()
cur.close()
conn.close()
return result
def city_total(db):
"""(str) -> list of tuple
Much better!
3
cur.execute(select City, Days from Precipitation where Days > ?,
(180,))
Lets write a function that returns all cities from Precipitation table where the amount of snow
falling is larger than 200:
def city_snow_over_200(db):
"""Return a list of cities from the Precipitation table in the
database db, where the amount of snowfall is > 200 cm.
It might be nice to be able to make a more generalized function that allows us to specify an
amount for the snow and total precipitation as an argument in the function. Complete the function
snow total over below.
def snow_total_over(db, x, y):
"""(str, number, number) -> list of tuple
Return the cities and snowfall amounts of the cities with at least
x cm of snowfall and y total precipitation from the Precipitation table of
database db.
"""
conn = sqlite3.connect(db)
cur = conn.cursor()
(*)
result = cur.fetchall()
cur.close()
conn.close()
return result
Q. Can we use the helper function run query for line (*)?
A.
Lets improve run query() to allow us to pass query arguments. Notice the header: we are
introducing a new way to pass parameters, args=None means if there isnt a third argument
passed to run query() then assume that the value is None.
def run_query_2(db, q, args=None):
"""(str, str, tuple) -> list of tuple
Return the results of running query q with arguments args on
database db."""
conn = sqlite3.connect(db)
cur = conn.cursor()
if args is None:
else:
data = cur.fetchall()
4
cur.close()
conn.close()
return data
Notice that we can still use run query 2 as we did run query for function city snow:
def city_snow_3(db):
"""(str) -> list of tuple
Return a list of tuples [(city, snow), ...] from the Precipitation
table in the database db (name/path to a database)."""
5
5 Joins
Sometimes we want to select data from two or more different tables. Consider the following two
tables, one which lists library card ID number with name and one which lists library card ID
number with books on loan from the library.
Suppose that we want to list the books taken out of the library for each student. This requires
using one table to look up into another. We can do this by joining the tables. The join of two
tables is their cross product. This means you match up every row of the left table with every row
of the right table. The symbol for join is o
n. Here is the result of Library ID on Library Loans:
ID Name ID Book
99110022 Homer 99110022 NULL
99110022 Homer 99110033 Updos for the Modern Woman
99110022 Homer 99110044 The Etymology of Slang
99110022 Homer 99110055 Quantum Physics for Minors
99110022 Homer 99110055 LSAT Prep Guide
99110033 Marge 99110022 NULL
99110033 Marge 99110033 Updos for the Modern Woman
99110033 Marge 99110044 The Etymology of Slang
99110033 Marge 99110055 Quantum Physics for Minors
99110033 Marge 99110055 LSAT Prep Guide
99110044 Bart 99110022 NULL
99110044 Bart 99110033 Updos for the Modern Woman
99110044 Bart 99110044 The Etymology of Slang
99110044 Bart 99110055 Quantum Physics for Minors
99110044 Bart 99110055 LSAT Prep Guide
99110055 Lisa 99110022 NULL
99110055 Lisa 99110033 Updos for the Modern Woman
99110055 Lisa 99110044 The Etymology of Slang
99110055 Lisa 99110055 Quantum Physics for Minors
99110055 Lisa 99110055 LSAT Prep Guide
Notice that in this case, the only lines that are useful to us are the ones where the library IDs
match. To use this table we need to learn:
6
2. How would we select the set of Names and Books taken out by a person?
Consider another table Temperature related to our Precipitation table (see page 11 of this
handout) populated from the file temperature.txt. Firstly, lets add it to our database:
Parameters:
db: name/path to a database
# connect to database
conn = sqlite3.connect(db)
# get cursor
cur = conn.cursor()
7
# commit the changes to the database
conn.commit()
We will also use a table Geography populated by the data in the file geography.txt. Can you
do this one yourself?
Parameters:
db: name/path to a database
Write a select statement to pass to run query2 that returns the names of cities which have
an average low temperature less than 1 and at least 250cm of snow. What tables do you need
to join here?
8
Write a select statement to pass to run query2 that returns the names of cities and
provinces, such that the city has average high temperature over 10 and total precipitation
less than 900. What tables do you need to join here?
Q. Write a query that returns the names of cities which have the same average low temperatures.
Notice that there are duplicates, but the order is permuted we can get rid of these by doing
some post processing (working with the results after). Now consider the following example:
Q. Write a query that returns the names of the provinces which have cities with negative average
low temperature.
Notice that this returns duplicates. We can get rid them by using select distinct.
5.3 Aggregation
There are several built-in column functions that we can use: they are avg, min, max, sum, count.
They are called aggregate functions and are used as select function name(column) etc...
Q. Use avg to find the average snowfall amount in table Precipitation.
Q. Write a query that returns the lowest average low temperature in the coldest month in each
province which has a city with snowfall of at least 150.
9
Python Commands
import sqlite3: use SQLite to access a database.
cur = conn.cursor(): Get a cursor to work with the database and name it cur.
cur.execute("SQL command"): Execute the given SQL command (string) on the database
accessed with the cursor cur.
cur.fetchall(): Get a list of rows of the table (tuples) from the columns that have been
selected.
cur.fetchone(): Get one row of the table (tuple) from the columns that have been selected.
10
precipitation.txt geography.txt
temperature.txt
11