計算機科学のブログ

SQL - Python - データとテーブル - あらゆるものにふさわしい場所 - create table

Head First SQL ―頭とからだで覚えるSQLの基本Lynn Beighley(著)、 佐藤 直生(監訳)、 松永 多苗子(翻訳)、 オライリージャパンの 1章(データとテーブル - あらゆるものにふさわしい場所)、p.29(SQLマグネット)の解答を求めてみる。

schema2.sql

drop table my_contacts;
create table my_contacts (
    email text,
    birthday date,
    first_name text,
    last_name text,
    interests text,
    seeking text,
    status text,
    profession text,
    location text,
    gender text
);

コード

sample2.py

#! /usr/bin/env python3
import sqlite3

con = sqlite3.connect('sample.db')
cur = con.cursor()
with open('schema2.sql') as f:
    cur.executescript(f.read())
print(cur)
cur.close()
con.close()

入出力結果(Terminal, Zsh)

% ./sample2.py
<sqlite3.Cursor object at 0x102d2fcc0>
%