Skip to main content

3.5.1 Database Roadmap: Tables That Live Beyond One File

This chapter is optional in Chapter 3. Read it when you want to understand where real project data lives before it becomes a CSV or DataFrame.

Look at the Database Map First

Database elective learning roadmap

Remember the simple comparison:

CSV or DataFrameDatabase
good for one local analysisgood for long-term shared data
easy to move aroundsafer for querying, permissions, and updates
usually one file or one tableoften many linked tables

You do not need to become a database administrator. You only need enough database literacy to query data and connect it to Python.

Run One SQLite Query Once

Create sqlite_first_loop.py. It uses sqlite3, which comes with Python.

import sqlite3

conn = sqlite3.connect(":memory:")
conn.execute("CREATE TABLE orders (category TEXT, amount INTEGER)")
conn.executemany(
"INSERT INTO orders VALUES (?, ?)",
[("book", 120), ("tool", 80), ("book", 150)],
)

rows = conn.execute(
"SELECT category, SUM(amount) AS total FROM orders GROUP BY category ORDER BY category"
).fetchall()

for category, total in rows:
print(category, total)

Expected output:

book 270
tool 80

This is the database loop: create a table, insert rows, ask a question with SQL, and receive a result table.

Learn in This Order

OrderReadWhat to practice
13.5.2 Relational Database Basicstables, rows, columns, primary keys, foreign keys
23.5.3 SQL BasicsSELECT, WHERE, JOIN, GROUP BY
33.5.4 Python Database Operationssqlite3, Pandas read/write, query results
43.5.5 Database Designsplit tables, avoid duplication, keep relationships clear

Pass Check

You pass this optional subchapter when you can explain why a database is different from a CSV, write one SELECT ... GROUP BY query, and read the result from Python.