Databases and SQL - Library edition

Programming with Databases

Learning Objectives

  • Write short programs that execute SQL queries.
  • Trace the execution of a program that contains an SQL query.
  • Explain why most database applications are written in a general-purpose language rather than in SQL.

To close, let’s have a look at how to access a database from a general-purpose programming language like Python. Other languages use almost exactly the same model: library and function names may differ, but the concepts are the same.

Here’s a short Python program that selects author names from an SQLite database stored in a file called swclib.db:

import sqlite3
connection = sqlite3.connect("swclib.db")
cursor = connection.cursor()
cursor.execute("SELECT personal, family FROM Authors;")
results = cursor.fetchall()
for r in results:
    print r
cursor.close()
connection.close()
(Kevin E.,Kline)
(Daniel,Kline)
(Brand,Hunt)
...

The program starts by importing the sqlite3 library. If we were connecting to MySQL, DB2, or some other database, we would import a different library, but all of them provide the same functions, so that the rest of our program does not have to change (at least, not much) if we switch from one database to another.

Line 2 establishes a connection to the database. Since we’re using SQLite, all we need to specify is the name of the database file. Other systems may require us to provide a username and password as well. Line 3 then uses this connection to create a cursor. Just like the cursor in an editor, its role is to keep track of where we are in the database.

On line 4, we use that cursor to ask the database to execute a query for us. The query is written in SQL, and passed to cursor.execute as a string. It’s our job to make sure that SQL is properly formatted; if it isn’t, or if something goes wrong when it is being executed, the database will report an error.

The database returns the results of the query to us in response to the cursor.fetchall call on line 5. This result is a list with one entry for each record in the result set; if we loop over that list (line 6) and print those list entries (line 7), we can see that each one is a tuple with one element for each field we asked for.

Finally, lines 8 and 9 close our cursor and our connection, since the database can only keep a limited number of these open at one time. Since establishing a connection takes time, though, we shouldn’t open a connection, do one operation, then close the connection, only to reopen it a few microseconds later to do another operation. Instead, it’s normal to create one connection that stays open for the lifetime of the program.

Queries in real applications will often depend on values provided by users. For example, this function takes an author ID as a parameter and returns the name of the corresponding author in the database:

def get_name(database_file, author_ident):
    query = "SELECT personal || ' ' || family FROM Authors WHERE author_ID='" + author_ident + "';"

    connection = sqlite3.connect(database_file)
    cursor = connection.cursor()
    cursor.execute(query)
    results = cursor.fetchall()
    cursor.close()
    connection.close()

    return results[0][0]

print "full name for author #20:", get_name('swclib.db', 20)
full name for author #20: Ross Mistry

We use string concatenation on the first line of this function to construct a query containing the author_ID we have been given. This seems simple enough, but what happens if someone gives us this string as input?

20'; DROP TABLE Items; SELECT '

It looks like there’s garbage after the ID of the author, but it is very carefully chosen garbage. If we insert this string into our query, the result is:

SELECT personal || ' ' || family FROM Authors WHERE author_ID='20'; DROP TABLE Items; SELECT '';"

If we execute this, it will erase one of the tables in our database.

This is called an SQL injection attack, and it has been used to attack thousands of programs over the years. In particular, many web sites that take data from users insert values directly into queries without checking them carefully first.

Since a villain might try to smuggle commands into our queries in many different ways, the safest way to deal with this threat is to replace characters like quotes with their escaped equivalents, so that we can safely put whatever the user gives us inside a string. We can do this by using a prepared statement instead of formatting our statements as strings. Here’s what our example program looks like if we do this:

def get_name(database_file, author_ident):
    query = "SELECT personal || ' ' || family FROM Authors WHERE author_ID=?;"

    connection = sqlite3.connect(database_file)
    cursor = connection.cursor()
    cursor.execute(query, [author_ident])
    results = cursor.fetchall()
    cursor.close()
    connection.close()

    return results[0][0]

print "full name for author #20:", get_name('swclib.db', 20)
full name for author #20: Ross Mistry

The key changes are in the query string and the execute call. Instead of formatting the query ourselves, we put question marks in the query template where we want to insert values. When we call execute, we provide a list that contains as many values as there are question marks in the query. The library matches values to question marks in order, and translates any special characters in the values into their escaped equivalents so that they are safe to use.

Filling a Table vs. Printing Values

Write a Python program that creates a new database in a file called visits.db containing a single table called Visits, with a single field called daily_visits, and inserts 100,000 random numbers between 1 and 250. How long does it take this program to run? How long does it take to run a program that simply writes those random numbers to a file?

Filtering in SQL vs. Filtering in Python

Write a Python program that creates a new database called backup.db with the same structure as visits.db and copies all the values greater than 50 from visits.db to backup.db. Which is faster: filtering values in the query, or reading everything into memory and filtering in Python?