AlgoMaster Logo

Database Access - Quiz

Last Updated: December 6, 2025

Database Access Exercises

31 quizzes

1
Code Completion

Complete the code so that a connection to an SQLite database is created and assigned to db_conn

python
1
db_conn = sqlite3.('app.db')

Click an option to fill the blank:

2
Code Completion

Complete the code so that a row is inserted using a safe, parameterized query instead of string formatting

python
1
cursor.execute('INSERT INTO users(name, age) VALUES(?, ?)', )

Click an option to fill the blank:

3
Code Completion

Complete the code so that the SQLAlchemy engine logs all generated SQL statements

python
1
engine = create_engine('sqlite:///shop.db', =True)

Click an option to fill the blank:

4
Multiple Choice

In a relational database, what does a primary key uniquely identify?

5
Multiple Choice

Which statement best describes a relational database?

6
Multiple Choice

Which Python module from the standard library is used to work with SQLite databases?

7
Multiple Choice

What does connection.commit() do in the context of sqlite3?

8
Multiple Choice

When connecting to an SQLite database with sqlite3.connect('data.db'), what happens if 'data.db' does not exist?

9
Multiple Choice

Which method on a sqlite3 cursor object is used to run a single SQL command?

10
Multiple Choice

Why are parameterized queries preferred over building SQL strings with string concatenation?

11
Multiple Choice

In SQLAlchemy ORM, what does a model class typically represent?

12
Multiple Choice

What is the role of a SQLAlchemy Session?

13
Multiple Choice

Which connection parameter is LEAST likely to be needed when using sqlite3 with a local .db file?

14
Multiple Choice

In an ORM, what is a one-to-many relationship?

15
Sequencing

Order the steps to insert a new row into an SQLite database using sqlite3.

Drag and drop to reorder, or use the arrows.

16
Sequencing

Order the steps to define a SQLAlchemy ORM model and create its table in the database.

Drag and drop to reorder, or use the arrows.

17
Output Prediction

What is the output of this sqlite3 code after inserting two users?

1import sqlite3
2conn = sqlite3.connect(':memory:')
3cur = conn.cursor()
4cur.execute('CREATE TABLE users(id INTEGER PRIMARY KEY, name TEXT)')
5cur.execute('INSERT INTO users(name) VALUES(?)', ('Alice',))
6cur.execute('INSERT INTO users(name) VALUES(?)', ('Bob',))
7conn.commit()
8cur.execute('SELECT COUNT(*) FROM users')
9count = cur.fetchone()[0]
10print(count)
18
Output Prediction

What is the output of this code that uses a parameterized SELECT?

1import sqlite3
2conn = sqlite3.connect(':memory:')
3cur = conn.cursor()
4cur.execute('CREATE TABLE products(id INTEGER PRIMARY KEY, name TEXT, price INTEGER)')
5cur.execute('INSERT INTO products(name, price) VALUES(?, ?)', ('Book', 10))
6cur.execute('INSERT INTO products(name, price) VALUES(?, ?)', ('Pen', 2))
7conn.commit()
8cur.execute('SELECT name FROM products WHERE price > ?', (5,))
9row = cur.fetchone()
10print(row[0])
19
Output Prediction

What is the output of this code querying with SQLAlchemy ORM?

1from sqlalchemy import create_engine, Column, Integer, String
2from sqlalchemy.orm import declarative_base, sessionmaker
3engine = create_engine('sqlite:///:memory:')
4Base = declarative_base()
5class User(Base):
6    __tablename__ = 'users'
7    id = Column(Integer, primary_key=True)
8    name = Column(String)
9Base.metadata.create_all(engine)
10Session = sessionmaker(bind=engine)
11session = Session()
12user = User(name='Alice')
13session.add(user)
14session.commit()
15result = session.query(User).filter_by(name='Alice').first()
16print(result.name)
20
Output Prediction

What is the output when selecting a missing row with fetchone()?

1import sqlite3
2conn = sqlite3.connect(':memory:')
3cur = conn.cursor()
4cur.execute('CREATE TABLE items(id INTEGER PRIMARY KEY, label TEXT)')
5cur.execute('INSERT INTO items(label) VALUES(?)', ('Box',))
6conn.commit()
7cur.execute('SELECT label FROM items WHERE id = ?', (2,))
8row = cur.fetchone()
9print(row is None)
21
Output Prediction

What is the output of this code that updates a row and reads it back?

1import sqlite3
2conn = sqlite3.connect(':memory:')
3cur = conn.cursor()
4cur.execute('CREATE TABLE counters(id INTEGER PRIMARY KEY, value INTEGER)')
5cur.execute('INSERT INTO counters(value) VALUES(?)', (1,))
6conn.commit()
7cur.execute('UPDATE counters SET value = value + 4 WHERE id = 1')
8conn.commit()
9cur.execute('SELECT value FROM counters WHERE id = 1')
10val = cur.fetchone()[0]
11print(val)
22
Bug Spotting

Find the bug in this sqlite3 code that should insert and fetch a user by email.

Click on the line(s) that contain the bug.

python
1
import sqlite3
2
conn = sqlite3.connect(':memory:')
3
cur = conn.cursor()
4
cur.execute('CREATE TABLE users(id INTEGER PRIMARY KEY, email TEXT)')
5
cur.execute("INSERT INTO users(email) VALUES('%s')" % ('user@example.com',))
6
conn.commit()
7
cur.execute('SELECT email FROM users WHERE email = ?', 'user@example.com')
8
row = cur.fetchone()
9
print(row[0])
23
Bug Spotting

Find the bug in this SQLAlchemy ORM code that should create and query a Product.

Click on the line(s) that contain the bug.

python
1
from sqlalchemy import create_engine, Column, Integer, String
2
from sqlalchemy.orm import declarative_base, sessionmaker
3
engine = create_engine('sqlite:///:memory:')
4
Base = declarative_base()
5
class Product(Base):
6
    __tablename__ = 'products'
7
    id = Column(Integer, primary_key=True)
8
    name = Column(String)
9
 
10
Session = sessionmaker()
11
session = Session(bind=engine)
12
item = Product(name='Table')
13
session.add(item)
14
result = session.query(Product).filter_by(name='Table').first()
15
print(result.name)
24
Matching

Match each SQL command with its primary purpose.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

25
Matching

Match each SQLAlchemy concept with its description.

Click an item on the left, then click its match on the right. Click a matched item to unmatch.

26
Fill in the Blanks

Complete the code to safely insert a new customer into an SQLite database using a parameterized query.

python
1
import sqlite3
2
connection = sqlite3.connect('customers.db')
3
cursor = connection.()
4
cursor.execute('INSERT INTO customers(name, city) VALUES(?, ?)', )
5
connection.()
6
connection.close()

Click an option to fill blank 1:

27
Fill in the Blanks

Complete the SQLAlchemy code to query all users older than a given age.

python
1
from sqlalchemy import create_engine, Column, Integer, String
2
from sqlalchemy.orm import declarative_base, sessionmaker
3
engine = create_engine('sqlite:///users.db')
4
Base = declarative_base()
5
class User(Base):
6
__tablename__ = 'users'
7
id = Column(Integer, primary_key=True)
8
name = Column(String)
9
age = Column(Integer)
10
Base.metadata.(engine)
11
Session = sessionmaker(bind=engine)
12
session = Session()
13
older_users = session.query(User).(User.age > 30).all()
14
print(len(older_users))

Click an option to fill blank 1:

28
Fill in the Blanks

Complete the code so that a safe SELECT is executed to get a user by name.

python
1
import sqlite3
2
conn = sqlite3.connect('app.db')
3
cur = conn.cursor()
4
name = 'Alice'
5
cur.execute('SELECT id FROM users WHERE name = ?', )
6
row = cur.()
7
if row:
8
print(row[0])
9
conn.close()

Click an option to fill blank 1:

29
Fill in the Blanks

Complete the ORM code to add a new BlogPost and persist it.

python
1
from sqlalchemy import create_engine, Column, Integer, String
2
from sqlalchemy.orm import declarative_base, sessionmaker
3
engine = create_engine('sqlite:///blog.db')
4
Base = declarative_base()
5
class BlogPost(Base):
6
__tablename__ = 'posts'
7
id = Column(Integer, primary_key=True)
8
title = Column(String)
9
Base.metadata.create_all(engine)
10
Session = sessionmaker(bind=engine)
11
session = Session()
12
post = BlogPost(title='First')
13
session.(post)
14
session.()

Click an option to fill blank 1:

30
Hotspot Selection

Click the line that attempts to build an SQL query using unsafe string concatenation.

Click on the line to select.

python
1
import sqlite3
2
conn = sqlite3.connect(':memory:')
3
cur = conn.cursor()
4
username = input('Username: ')
5
query = "SELECT * FROM users WHERE name = '" + username + "'"
6
cur.execute(query)
7
row = cur.fetchone()
8
print(row)
31
Hotspot Selection

Click the line responsible for actually committing changes to the SQLite database.

Click on the line to select.

python
1
import sqlite3
2
conn = sqlite3.connect('inventory.db')
3
cur = conn.cursor()
4
cur.execute('CREATE TABLE IF NOT EXISTS items(id INTEGER PRIMARY KEY, name TEXT)')
5
cur.execute('INSERT INTO items(name) VALUES(?)', ('Box',))
6
conn.commit()
7
conn.close()

Premium Content

Subscribe to unlock full access to this content and more premium articles.