← QCE Digital Solutions Unit 2

Connecting Python to a SQLite database

Opens a SQLite database file from Python and returns a connection. Call conn.cursor() on it to get a cursor for running SQL, and conn.close() when finished.

Understanding Connecting Python to a SQLite database in QCE Digital Solutions Unit 2

What it does: import sqlite3 loads the built-in module that provides connect() and the other database tools. conn.close() ends the connection to the database file and frees it up once the work is done. The cursor is made from the connection object (conn), not from the sqlite3 module. SQLite creates the file if it is missing. This is why a misspelt file name gives you a new empty database instead of an error. SQL is run with cur.execute(). conn.cursor() takes no SQL; it only creates the cursor.

Common mistakes: conn.cursor is missing its brackets, so it only refers to the method and does not call it. It should be cur = conn.cursor(). The cursor is created from the connection object, not from the sqlite3 module. It should be cur = conn.cursor().

Class: _____________________  ·  Name: _____________________  ·  Date: _________

Free printable worksheet with full answer key — or add this module to a class and your students practise it interactively.

This module's key terms are also in a printable crossword (Modules 337–409) and word search (Modules 234–409).

Take this worksheet further — free with Cloodit

Each concept is reinforced from every angle — learned, then applied, then tested.

Learning
Duo
Which line opens a SQLite database file called school.db from Python?
conn = sqlite3.connect('school.db')conn = sqlite3.open('school.db')
Matches
conn = sqlite3.connect(school.db)
↓
The file name is not in quotes, so Python treats school.db as a variable name instead of a string. It should be 'school.db'.
Applying
Meanings
Term
sqlite3.connect()
Meaning
Opens a SQLite database file from Python and returns a connection. Call conn.cursor() on it to get a cursor for running SQL, and conn.close() when finished.
Practice
import sqlite3
Testing
Gaps
import
Check Answer
Spots
2conn = sqlite3.connect("school.db")
3cur = conn.cursor

Cloodit turns curriculum-aligned coding content into journeys students work through one biome at a time.

The 'Desert Jerboa' badge — High Jumper, one of Cloodit's 106 collectible badges
Students earn collectable badges

Over 100 of them, across seven natural biomes.

Study Buddy
Students build a real study resource

Turns their work into revision material ready for Flash Cards and Exam study.

Diamond Dash — one of Cloodit's live, competitive Sprint games
Students lock in with Sprints

Live, competitive practice for the start of the lesson or a teacher-led lesson finisher.

A Cloodit certificate of achievement, ready to personalise and print
Custom printable certificates

Honour your top performers, or encourage a student who's trying hard — ready to print in seconds.

Quick Questions

1. Which line opens a SQLite database file called school.db from Python?
A. conn = sqlite3.connect('school.db') ✓
B. conn = sqlite3.open('school.db')
sqlite3.connect() opens the database file and returns a connection. There is no sqlite3.open() function.
2. Which statement must appear at the top of the program before you can use sqlite3.connect()?
A. import connect
B. import sqlite3 ✓
The sqlite3 module must be imported first. connect is a function inside sqlite3, not a module you import.
3. Which line creates the cursor you use to run SQL?
A. cur = conn.cursor() ✓
B. cur = sqlite3.cursor()
The cursor is made from the connection object (conn), not from the sqlite3 module.
4. True or false: if school.db does not exist, sqlite3.connect('school.db') creates a new empty database file.
A. True ✓
B. False
SQLite creates the file if it is missing. This is why a misspelt file name gives you a new empty database instead of an error.
5. Which line correctly finishes with the database when your program is done?
A. cur.close_database()
B. conn.close() ✓
conn.close() closes the connection to the database file. The cursor has no close_database() method.

Match the Pairs

conn = sqlite3.connect('school.db') → Opens the school.db database file (creating it if missing) and stores the connection in conn
cur = conn.cursor() → Creates a cursor from the connection so the program can run SQL statements
Before Python can use SQLite, the sqlite3 module must be brought into the program → import sqlite3
When the program has finished with the database, the connection should be closed → conn.close()
conn = sqlite3.connect(school.db) → The file name is not in quotes, so Python treats school.db as a variable name instead of a string. It should be 'school.db'.
cur = sqlite3.cursor() → The cursor is made from the connection, not the module. It should be conn.cursor().

Key Term

sqlite3.connect()
Opens a SQLite database file from Python and returns a connection. Call conn.cursor() on it to get a cursor for running SQL, and conn.close() when finished.

Fill in the Blanks

1. import sqlite3
Hint: Type the name of the built-in Python module used to work with SQLite databases.
2. conn = sqlite3.connect("school.db")
Hint: Type the function that opens the database file and returns a connection.
3. conn = sqlite3.connect("school.db") cur = conn.cursor()
Hint: Type the connection method that creates the object used to run SQL.
4. cur.execute("CREATE TABLE pet(id INTEGER)")
Hint: Type the cursor method that sends an SQL statement to the database.
5. cur = conn.cursor() conn.close()
Hint: Type the connection method that ends the link to the database when you are finished.

Spot the Error

Question 1
A import sqlite3
B conn = sqlite3.connect("school.db")
C cur = conn.cursor
D conn.close()
Line C — conn.cursor is missing its brackets, so it only refers to the method and does not call it. It should be cur = conn.cursor().
Question 2
A import sqlite3
B conn = sqlite3.connect("a.db")
C cur = sqlite3.cursor()
D conn.close()
Line C — The cursor is created from the connection object, not from the sqlite3 module. It should be cur = conn.cursor().
Question 3
A import sqlite
B conn = sqlite3.connect("b.db")
C conn.close()
Line A — The module is called sqlite3, including the 3. Without it Python cannot find the module. It should be import sqlite3.
Question 4
A import sqlite3
B conn = sqlite3.connect("pets.db")
C cur = conn.cursor()
D sql = "CREATE TABLE p(id INT"
E cur.execute(sql)
Line D — The SQL text is missing its closing bracket after INT, so the CREATE TABLE statement is invalid. It should end with INT).
Question 5
A import sqlite3
B conn = sqlite3.connect(pets.db)
C cur = conn.cursor()
D conn.close()
Line B — The database file name must be a string in quotes. Without quotes Python treats pets.db as a variable. It should be connect("pets.db").

Student with a class code? Join your class →