Getting Database Drivers Right in a Student Project
This page describes what good looks like, using a hypothetical student project. It is not a testimonial or a real story; it is a model to compare your own code against. The project is a small quiz app written in Python with SQLite.
The habits this project gets right
1. One place that opens connections
All database access goes through a small module, db.py, with a single function that opens a connection:
import sqlite3
def get_connection():
conn = sqlite3.connect("quiz.db")
conn.row_factory = sqlite3.Row # rows behave like dictionaries
conn.execute("PRAGMA foreign_keys = ON")
return conn
Why it is good: settings live in one spot. Switching on foreign key checks, which SQLite leaves off by default, happens for every connection automatically.
2. Placeholders every time
def get_questions(conn, topic):
return conn.execute(
"SELECT id, text FROM questions WHERE topic = ?", (topic,)
).fetchall()
Why it is good: user input never becomes part of the SQL, so SQL injection is ruled out.
3. Connections are closed
with get_connection() as conn:
...
conn.close()
In Python's sqlite3, using the connection in a with block commits or rolls back the transaction; closing is a separate step. The project closes connections explicitly when a request finishes.
Why it is good: no leftover connections or locked files.
4. Changes are grouped in transactions
When a student submits a quiz, the app saves the attempt and every answer together. If anything fails, nothing is saved, so there are never half-saved attempts.
5. Errors are handled, not hidden
The app catches sqlite3.IntegrityError when a rule is broken, for example a duplicate username, and shows a clear message. Other errors are logged so the student can debug them.
6. The database path is configurable
The file name comes from an environment variable with a sensible default, so tests can use a separate database file.
A comparison table
| Habit | Typical first attempt | What good looks like |
|---|---|---|
| Opening connections | Copied into every function | One helper function |
| Passing values | Glued into strings | Placeholders |
| Closing | Forgotten | Closed after each request |
| Multi-step saves | Separate commits | One transaction |
| Errors | Ignored or crash | Caught and explained |
| Config | Hard-coded paths | Environment variable |
How to apply this to your project
- Search your code for
connect(. If it appears in many places, create a helper. - Search for SQL strings joined with
+or f-strings with values. Replace with placeholders. - Check that every code path closes or returns its connection.
- Find any save that touches more than one table and wrap it in a transaction.
Same ideas, other languages
The habits carry over to Node.js, Java, C# and PHP. Only the syntax changes. Compare the patterns in One Query, Five Drivers.
A free learning page for beginners. Practise on a test database, never on real customer data, and check the official documentation for your database version.
Illustrative example: the organisations, people, projects and results described on this page are hypothetical. They do not describe a real client or engagement, and no consulting services are offered here.