計算機科学のブログ

ほしい物リスト

Python - SQLite - Databases: Getting Organized - tables

Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly GuidePaul Barry(著)、 O’Reilly Mediaの Chapter 12.(Databases: Getting Organized)、EXERCISE(505/682)の解答を求めてみる。

Jupyter(コード、入出力結果)

webapp/CreateDatabaseTables.ipynb

import DBcm

db_details = 'CoachDB.sqlite'
sql = """
create table if not exists swimmers (
    id integer not null primary key autoincrement,
    name varchar(32) not null,
    age integer not null
)
"""
with DBcm.UseDatabase(db_details) as db:
    db.execute(sql)
with DBcm.UseDatabase(db_details) as db:
    db.execute('pragma table_list')
    results = db.fetchall()
results
[('main', 'sqlite_sequence', 'table', 2, 0, 0),
 ('main', 'swimmers', 'table', 3, 0, 0),
 ('main', 'sqlite_schema', 'table', 5, 0, 0),
 ('temp', 'sqlite_temp_schema', 'table', 5, 0, 0)]