CSC 282 · Database Programming
Our notes
2 min readHow programs use a database
- Connect with a driver (host, database name, user, password: kept in a config file, never in the code you share).
- Send an SQL statement.
- Read the result rows (a cursor or result set) and use them.
- 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
-
Open Database Connectivity
(opens en.wikipedia.org in a new tab)
Wikipedia, Open Database Connectivity, CC BY-SA 4.0 · licence
-
SQL injection
(opens en.wikipedia.org in a new tab)
Wikipedia, SQL injection, CC BY-SA 4.0 · licence
-
SQLite
(opens en.wikipedia.org in a new tab)
Wikipedia, SQLite, CC BY-SA 4.0 · licence
-
NOUN CIT 304: Data Management
NOUN course book (PDF, about 6.9 MB: best on Wi-Fi). Files, databases, the relational model and SQL.
(opens nou.edu.ng in a new tab)
Watch
-
Best pick Python SQLite Tutorial: Complete Overview - Creating a Database, Table, and Running Queries
30 min · ≈ 135 MB · about ₦76
(opens youtube.com in a new tab)
-
SQLite Introduction - Beginners Guide to SQL and Databases
22 min · ≈ 99 MB · about ₦56
(opens youtube.com in a new tab)
-
SQLite in Python || Python Tutorial || Learn Python Programming
10 min · ≈ 45 MB · about ₦25
(opens youtube.com in a new tab)
-
Python SQLite Tutorial: Databases for Beginners in 10 Minutes
4 min · ≈ 18 MB · about ₦10
(opens youtube.com in a new tab)
-
SQLite Databases With Python - Full Course
1 h 30 min · ≈ 405 MB · about ₦227
(opens youtube.com in a new tab)
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.