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

HabitTypical first attemptWhat good looks like
Opening connectionsCopied into every functionOne helper function
Passing valuesGlued into stringsPlaceholders
ClosingForgottenClosed after each request
Multi-step savesSeparate commitsOne transaction
ErrorsIgnored or crashCaught and explained
ConfigHard-coded pathsEnvironment variable

How to apply this to your project

  1. Search your code for connect(. If it appears in many places, create a helper.
  2. Search for SQL strings joined with + or f-strings with values. Replace with placeholders.
  3. Check that every code path closes or returns its connection.
  4. 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.