This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

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...
┌─── Python Source Code ─────────────────────────────────────
│  1 | import sqlite3
│  2 | # Connect to an in-memory database
│  3 | conn = sqlite3.connect(':memory:')
│  4 | cur = conn.cursor()
│  5 | # Create schema for library system
│  6 | schema = '''
│  7 | CREATE TABLE authors (
│  8 | id INTEGER PRIMARY KEY AUTOINCREMENT,
│  9 | name TEXT NOT NULL
│  10 | );
│  11 | CREATE TABLE books (
│  12 | id INTEGER PRIMARY KEY AUTOINCREMENT,
│  13 | title TEXT NOT NULL,
│  14 | author_id INTEGER,
│  15 | FOREIGN KEY(author_id) REFERENCES authors(id)
│  16 | );
│  17 | '''
│  18 | cur.executescript(schema)
│  19 | # Insert sample records
│  20 | authors = [('Jane Austen',), ('Mark Twain',), ('J.K. Rowling',), ('George Orwell',), ('Leo Tolstoy',)]
│  21 | cur.executemany('INSERT INTO authors (name) VALUES (?)', authors)
│  22 | books = [
│  23 | ('Pride and Prejudice', 1),
│  24 | ('Adventures of Huckleberry Finn', 2),
│  25 | ('Harry Potter and the Sorcerer\'s Stone', 3),
│  26 | ('1984', 4),
│  27 | ('War and Peace', 5),
│  28 | ]
│  29 | cur.executemany('INSERT INTO books (title, author_id) VALUES (?, ?)', books)
│  30 | # Generate SQL dump
│  31 | dump = ""
│  32 | for line in conn.iterdump():
│  33 | dump += line + "\n"
│  34 | # Save dump to file
│  35 | with open('db_migration.sql', 'w') as f:
│  36 | f.write(dump)
│  37 | conn.close()
└────────────────────────────────────────────────────────────
The SQLite schema for the library system has been created, five sample records have been inserted, and a SQL dump has been generated and saved to 'db_migration.sql'. If you need to review the dump or perform further operations, please let me know!