2014年9月14日日曜日

開発環境

Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley(著)、 佐藤 直生 (監訳)、 松永 多苗子 (翻訳)、オライリージャパン)の8章(結合と複数テーブル操作: みんなでうまくやれないの?)、自分で考えてみよう(p.348)を解いてみる。

自分で考えてみよう(p.348)

コード(BBEdit, Emacs)

sample307.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

import sqlite3

connection = sqlite3.connect('gregs_list.sqlite')
cursor = connection.cursor()

cursor.execute("""SELECT * FROM {0}""".format('my_contacts'))
print(tuple(map(lambda header: header[0], cursor.description)))

cursor.execute("""
ALTER TABLE my_contacts
ADD COLUMN interest1 VARCHAR(10)
""")
cursor.execute("""
ALTER TABLE my_contacts
ADD COLUMN interest2 VARCHAR(10)
""")
cursor.execute("""
ALTER TABLE my_contacts
ADD COLUMN interest3 VARCHAR(10)
""")
cursor.execute("""
ALTER TABLE my_contacts
ADD COLUMN interest4 VARCHAR(10)
""")

cursor.execute("""SELECT * FROM {0}""".format('my_contacts'))
print(tuple(map(lambda header: header[0], cursor.description)))

connection.commit()
connection.close()

入出力結果(Terminal, IPython)

$ ./sample348.py
('last_name', 'first_name', 'email', 'gender', 'birthday', 'profession', 'location', 'status', 'interests', 'seeking')
('last_name', 'first_name', 'email', 'gender', 'birthday', 'profession', 'location', 'status', 'interests', 'seeking', 'interest1', 'interest2', 'interest3', 'interest4')
$

0 コメント:

コメントを投稿