UNIHIVE

Study Hub UNIPORT CSC 282

CSC 282 · Database Programming

Programming with a database

How a program connects, sends SQL, reads results and stays safe with parameterised queries; a small practical program.

Our notes

2 min read

How programs use a database

  1. Connect with a driver (host, database name, user, password: kept in a config file, never in the code you share).
  2. Send an SQL statement.
  3. Read the result rows (a cursor or result set) and use them.
  4. Close the connection.

Languages provide libraries for this: Python's sqlite3 and mysql-connector, PHP's PDO, Java's JDBC, C#'s ADO.NET.

A complete small program (Python with SQLite)

    import sqlite3
    con = sqlite3.connect("results.db")
    cur = con.cursor()
    cur.execute("""CREATE TABLE IF NOT EXISTS result (
                       matric_no TEXT, course_code TEXT, score INTEGER,
                       PRIMARY KEY (matric_no, course_code))""")
    rows = [("U01", "CSC 282", 70), ("U02", "CSC 282", 55), ("U03", "CSC 282", 38)]
    cur.executemany("INSERT OR REPLACE INTO result VALUES (?, ?, ?)", rows)
    con.commit()
    course = input("Course code: ")
    cur.execute("SELECT matric_no, score FROM result WHERE course_code = ? ORDER BY score DESC",
                (course,))
    for matric, score in cur.fetchall():
        print(matric, score, "PASS" if score >= 40 else "FAIL")
    con.close()

Typing CSC 282 prints U01 70 PASS, U02 55 PASS, U03 38 FAIL.

SQL injection and parameters

Never build SQL by joining user input into the string:

    # DANGEROUS
    cur.execute("SELECT * FROM student WHERE surname = '" + name + "'")

If someone types x' OR '1'='1 the condition becomes always true and every student's record is returned; worse inputs can change or delete data. Use parameters (the ? placeholders above, or :name, or %s depending on the library): the driver sends the value separately, so it is always treated as data, never as SQL.

Good habits

  • Validate input in the program and keep constraints in the database.
  • Use transactions for multi-step changes.
  • Give the program's database user only the permissions it needs (no DROP for a reporting tool).
  • Store passwords as hashes (bcrypt, Argon2), never as plain text.
  • Handle errors: a failed insert should give the user a clear message, not a crash.
  • Back up the database file or server regularly and test restoring it.

The practical

The course practical asks you to build and use a small database: design tables, create them, load data, and write queries and a program or forms that insert, update, delete, search and report. Keep your SQL in a text file with comments, and test each statement on a few rows before running it on many.

Exams usually ask

  • Steps for a program to access a database.
  • What SQL injection is and how parameterised queries prevent it.
  • Write a short program or pseudocode that reads, updates or reports from a table.

Common mistake

Forgetting to commit. In many libraries, inserts and updates are lost when the connection closes unless con.commit() is called.

Written by the UniHive team in our own words. Spotted a mistake? Tell us on WhatsApp.

Read

Watch

We link to free material and never copy it. Every link was opened by our team before it went up; video data is a rough guide at 360p.

All CSC 282 topics

Report

Sign in to report this. It is free.

Is this your work (a lecturer, author or publisher)? Ask us to take it down.