← QCE Digital Solutions Unit 4

Connecting to a SQLite database with sqlite3

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.

Understanding Connecting to a SQLite database with sqlite3 in QCE Digital Solutions Unit 4

What it does: import sqlite3 loads the built-in SQLite library so that sqlite3.connect() can be used. conn.close() ends the connection to the database file once the program is done with it. connect() creates the database file if it is missing. This is why a typo in the file name can quietly make a new empty database. The cursor is made from the connection object (conn), not from the sqlite3 module. You need a connection before you can make a cursor, and you must run your SQL before closing the connection. The module name is all lowercase and includes the 3: sqlite3. Python is case sensitive.

Common mistakes: close is a method, so it needs brackets to run. It should be conn.close() to end the connection. A cursor is made from the connection object, not 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 334–401) and word search (Modules 127–401).

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?
conn = sqlite3.connect("school.db")conn = sqlite3.open("school.db")
Matches
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.
Applying
Meanings
Term
conn = sqlite3.connect("school.db")
Meaning
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.
Practice
import sqlite3
Testing
Gaps
sqlite3
Check Answer
Spots
3cur = conn.cursor()
4conn.close

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

The 'Butterfly' badge — Branch Hopper, 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?
A. conn = sqlite3.open("school.db")
B. conn = sqlite3.connect("school.db") ✓
sqlite3.connect(file) opens the database and returns a connection. There is no sqlite3.open() function.
2. What must you write at the top of a program before you can use sqlite3.connect()?
A. import sqlite3 ✓
B. pip install sqlite3
sqlite3 is part of Python's standard library, so you only need to import it. No pip install is needed.
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 ✓
connect() creates the database file if it is missing. This is why a typo in the file name can quietly make a new empty database.
4. Which line creates a cursor so you can run SQL?
A. cur = conn.cursor() ✓
B. cur = sqlite3.cursor()
The cursor is made from the connection object (conn), not from the sqlite3 module.
5. What does conn.close() do?
A. Ends the connection to the database ✓
B. Deletes the database file
close() only ends the connection. The database file stays on disk with its saved data.

Match the Pairs

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.
cur = conn.cursor() → Makes a cursor from the connection, which is the object used to run SQL commands on the database.
Before a Python program can use SQLite, the sqlite3 library must be brought in with an import. → import sqlite3
When the program has finished with the database, the connection should be closed to end it. → conn.close()
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.
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().

Key Term

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.

Fill in the Blanks

1. import sqlite3
Hint: Type the keyword that loads a module into your program.
2. conn = sqlite3.connect("school.db")
Hint: Type the function name that opens the database file and returns a connection.
3. import sqlite3 conn = sqlite3.connect("school.db")
Hint: Type the name of the module that was imported on the line above.
4. conn = sqlite3.connect("school.db") cur = conn.cursor()
Hint: Type the method name that makes the object used to run SQL.
5. cur = conn.cursor() conn.close()
Hint: Type the method name that ends the connection to the database.

Spot the Error

Question 1
A import sqlite3
B conn = sqlite3.connect("school.db")
C cur = conn.cursor()
D conn.close
Line D — close is a method, so it needs brackets to run. It should be conn.close() to end the connection.
Question 2
A import sqlite3
B conn = sqlite3.connect("shop.db")
C cur = sqlite3.cursor()
D conn.close()
Line C — A cursor is made from the connection object, not the sqlite3 module. It should be cur = conn.cursor().
Question 3
A import sqlite3
B conn = sqlite3.connect("a.db")
C cur = conn.cursor()
D close.conn()
E print("Closed")
Line D — The method belongs to the connection, so the connection name comes first. It should be conn.close().
Question 4
A import sqlite
B conn = sqlite3.connect("log.db")
C cur = conn.cursor()
Line A — The module is called sqlite3, with the 3 on the end. It should be import sqlite3.
Question 5
A import sqlite3
B conn = sqlite3.open("school.db")
C cur = conn.cursor()
Line B — sqlite3 has no open() function. Use sqlite3.connect() to open the database file and get a connection.

Student with a class code? Join your class →