Lab 08: SQLite & Database
Objective
Time
Prerequisites
Tools
Lab Instructions
Step 1: sqlite3 Basics
docker run --rm zchencow/innozverse-python:latest python3 -c "
import sqlite3
# Connect (creates file or uses :memory: for temp DB)
conn = sqlite3.connect(':memory:')
conn.row_factory = sqlite3.Row # access columns by name
cur = conn.cursor()
# Create table
cur.execute('''
CREATE TABLE products (
id INTEGER PRIMARY KEY AUTOINCREMENT,
name TEXT NOT NULL,
price REAL NOT NULL CHECK(price > 0),
stock INTEGER NOT NULL DEFAULT 0,
category TEXT NOT NULL,
status TEXT NOT NULL DEFAULT \"active\",
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
)
''')
# Insert (always use ? placeholders — never f-strings!)
cur.executemany('''
INSERT INTO products (name, price, stock, category)
VALUES (?, ?, ?, ?)
''', [
('Surface Pro 12\"', 864.00, 15, 'Laptop'),
('Surface Pen', 49.99, 80, 'Accessory'),
('Office 365', 99.99, 999,'Software'),
('USB-C Hub', 29.99, 0, 'Accessory'),
('Surface Book 3', 1299.00,5, 'Laptop'),
])
conn.commit()
# Query all
rows = cur.execute('SELECT * FROM products ORDER BY price DESC').fetchall()
print(f'Total products: {len(rows)}')
for row in rows:
print(f' [{row[\"id\"]}] {row[\"name\"]:20s} \${row[\"price\"]:8.2f} stock={row[\"stock\"]}')
# WHERE + aggregation
avg = cur.execute('SELECT AVG(price) FROM products').fetchone()[0]
print(f'Average price: \${avg:.2f}')
by_cat = cur.execute('''
SELECT category, COUNT(*) as count, AVG(price) as avg_price
FROM products GROUP BY category ORDER BY category
''').fetchall()
for row in by_cat:
print(f' {row[\"category\"]:12s}: {row[\"count\"]} items, avg \${row[\"avg_price\"]:.2f}')
conn.close()
"Step 2: Transactions & Error Handling
Steps 3–8: Repository Pattern, Migrations, Full-Text Search, Pagination, Aggregation, Capstone
Summary
Pattern
Code
Notes
Further Reading
Last updated
