Connecting to a SQLite database with sqlite3

Tip: Choose Save as PDF in the print dialog to download. Use A4 paper size.

Connecting to a SQLite database with sqlite3

Name:
Date:
Class:

Quick Questions

Shade in the A or B circle for each question.

1. Which line opens a SQLite database file called school.db?
A conn = sqlite3.open("school.db")
B conn = sqlite3.connect("school.db")
2. What must you write at the top of a program before you can use sqlite3.connect()?
A import sqlite3
B pip install sqlite3
3. True or false: if school.db does not exist, sqlite3.connect("school.db") raises an error.
A True: the file must already exist
B False: it creates a new empty school.db file
4. Which line creates a cursor so you can run SQL?
A cur = conn.cursor()
B cur = sqlite3.cursor()
5. What does conn.close() do?
A Ends the connection to the database
B Deletes the database file

Match the Pairs

Draw a line connecting each item on the left to its match on the right.

Left columnRight column
1conn = sqlite3.connect("school.db") Aconnect() belongs to the sqlite3 library, so it needs the sqlite3. prefix. It should be sqlite3.connect("school.db"), otherwise Python raises a NameError.
2cur = conn.cursor() Bimport sqlite3
3Before a Python program can use SQLite, the sqlite3 library must be brought in with an import. COpens the database file school.db (creating it if it is missing) and returns a connection object stored in conn.
4When the program has finished with the database, the connection should be closed to end it. DMakes a cursor from the connection, which is the object used to run SQL commands on the database.
5conn = connect("school.db") EA cursor is made from the connection, not from the sqlite3 module. It should be conn.cursor(), using the connection returned by connect().
6cur = sqlite3.cursor() Fconn.close()

Definitions

conn = sqlite3.connect("school.db")

Fill in the Blanks

Complete each line of code by filling in the missing parts.

1.
______ sqlite3
2.
conn = sqlite3._______("school.db")
3.
import sqlite3 conn = _______.connect("school.db")
4.
conn = sqlite3.connect("school.db") cur = conn.______()
5.
cur = conn.cursor() conn._____()

Spot the Error

Shade in the letter of the line that contains an error.

Question 1
A import sqlite3
B conn = sqlite3.connect("school.db")
C cur = conn.cursor()
D conn.close
Question 2
A import sqlite3
B conn = sqlite3.connect("shop.db")
C cur = sqlite3.cursor()
D conn.close()
Question 3
A import sqlite3
B conn = sqlite3.connect("a.db")
C cur = conn.cursor()
D close.conn()
E print("Closed")
Question 4
A import sqlite
B conn = sqlite3.connect("log.db")
C cur = conn.cursor()
Question 5
A import sqlite3
B conn = sqlite3.open("school.db")
C cur = conn.cursor()

Answer Key

Connecting to a SQLite database with sqlite3
Section A: Quick Questions
Q1
B — conn = sqlite3.connect("school.db")
Q2
A — import sqlite3
Q3
B — False: it creates a new empty school.db file
Q4
A — cur = conn.cursor()
Q5
A — Ends the connection to the database
Section B: Match the Pairs
Q1
conn = sqlite3.connect("school.db") → Opens the database file school.db (creating it if it is missing) and returns a connection object stored in conn.
Q2
cur = conn.cursor() → Makes a cursor from the connection, which is the object used to run SQL commands on the database.
Q3
Before a Python program can use SQLite, the sqlite3 library must be brought in with an import. → import sqlite3
Q4
When the program has finished with the database, the connection should be closed to end it. → conn.close()
Q5
conn = connect("school.db") → connect() belongs to the sqlite3 library, so it needs the sqlite3. prefix. It should be sqlite3.connect("school.db"), otherwise Python raises a NameError.
Q6
cur = sqlite3.cursor() → A cursor is made from the connection, not from the sqlite3 module. It should be conn.cursor(), using the connection returned by connect().
Section C: Definitions
Q1
conn = sqlite3.connect("school.db") — Opens the SQLite database file school.db (creating it if missing) and returns a connection. Then conn.cursor() makes a cursor to run SQL, and conn.close() ends the connection.
Section D: Fill in the Blanks
Q1
import sqlite3
Q2
conn = sqlite3.connect("school.db")
Q3
import sqlite3 conn = sqlite3.connect("school.db")
Q4
conn = sqlite3.connect("school.db") cur = conn.cursor()
Q5
cur = conn.cursor() conn.close()
Section E: Spot the Error
Q1
D — close is a method, so it needs brackets to run. It should be conn.close() to end the connection.
Q2
C — A cursor is made from the connection object, not the sqlite3 module. It should be cur = conn.cursor().
Q3
D — The method belongs to the connection, so the connection name comes first. It should be conn.close().
Q4
A — The module is called sqlite3, with the 3 on the end. It should be import sqlite3.
Q5
B — sqlite3 has no open() function. Use sqlite3.connect() to open the database file and get a connection.