48 lines
3.5 KiB
Plaintext
Raw Normal View History

2026-01-29 06:54:10 +01:00
TASK: Create an SQLite schema for a library system (books, authors), insert 5 sample records, and generate a SQL dump to 'db_migration.sql'.
----------------------------------------
Loading...
2026-01-29 07:42:06 +01:00
┌─── Python Source Code ─────────────────────────────────────
│  1 | import sqlite3
│  2 | # Connect to an in-memory database
│  3 | conn = sqlite3.connect(':memory:')
│  4 | cur = conn.cursor()
2026-01-29 08:06:31 +01:00
│  5 | # Create tables
│  6 | cur.execute('''CREATE TABLE authors (
│  7 | id INTEGER PRIMARY KEY AUTOINCREMENT,
│  8 | name TEXT NOT NULL,
│  9 | birth_year INTEGER
│  10 | );''')
│  11 | cur.execute('''CREATE TABLE books (
2026-01-29 07:42:06 +01:00
│  12 | id INTEGER PRIMARY KEY AUTOINCREMENT,
│  13 | title TEXT NOT NULL,
│  14 | author_id INTEGER,
2026-01-29 08:06:31 +01:00
│  15 | published_year INTEGER,
│  16 | FOREIGN KEY(author_id) REFERENCES authors(id)
│  17 | );''')
│  18 | # Insert sample authors
│  19 | authors = [
│  20 | ('Jane Austen', 1775),
│  21 | ('Charles Dickens', 1812),
│  22 | ('Leo Tolstoy', 1828),
│  23 | ('Mark Twain', 1835),
│  24 | ('Virginia Woolf', 1882)
│  25 | ]
│  26 | cur.executemany('INSERT INTO authors (name, birth_year) VALUES (?, ?);', authors)
│  27 | # Insert sample books
│  28 | books = [
│  29 | ('Pride and Prejudice', 1, 1813),
│  30 | ('Great Expectations', 2, 1861),
│  31 | ('War and Peace', 3, 1869),
│  32 | ('Adventures of Huckleberry Finn', 4, 1884),
│  33 | ('Mrs. Dalloway', 5, 1925)
│  34 | ]
│  35 | cur.executemany('INSERT INTO books (title, author_id, published_year) VALUES (?, ?, ?);', books)
│  36 | # Generate dump
│  37 | with open('db_migration.sql', 'w') as f:
│  38 | for line in conn.iterdump():
│  39 | f.write('%s\n' % line)
│  40 | conn.close()
2026-01-29 07:42:06 +01:00
└────────────────────────────────────────────────────────────
2026-01-29 08:06:31 +01:00
The SQLite schema for a library system has been created, sample records inserted, and a SQL dump has been generated in the file 'db_migration.sql'.