開発環境
- OS X Mavericks - Apple、たまにFreeBSD 10(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4(プログラミング言語)
- SQLite (RDBMS(Relational Database Management System))
Head First SQL ―頭とからだで覚えるSQLの基本 (Lynn Beighley(著)、 佐藤 直生 (監訳)、 松永 多苗子 (翻訳)、オライリージャパン)の4章(賢いテーブル設計: 正規化の理由)、自分で考えてみよう(p.161)を解いてみる。
自分で考えてみよう(p.161)
コード(BBEdit, Emacs)
sample161.py
#!/usr/bin/env python3
#-*- coding: utf-8 -*-
import sqlite3
connection = sqlite3.connect('chapter4.sqlite')
cursor = connection.cursor()
def p(msg):
print(msg)
for row in cursor.fetchall():
print(row)
def p_all(table):
cursor.execute("""SELECT * FROM {0}""".format(table))
print(tuple(map(lambda header: header[0], cursor.description)))
p(table)
p_all('fish_info')
cursor.execute("""
SELECT * FROM fish_info
WHERE location LIKE '%NJ'
""")
p('ニュージャージー州(NZ)')
print()
p_all('fish_records')
cursor.execute("""
SELECT * FROM fish_records
WHERE state LIKE '%NJ'
""")
p('ニュージャージー州(NZ)')
connection.commit()
connection.close()
入出力結果(Terminal, IPython)
$ ./sample161.py
('common', 'species', 'location', 'weight')
fish_info
('bass, largemouth', 'M. salmoides', 'Montgomery Lake, GA', '22 lb 4 oz')
('walleye', 'S. vitreus', 'Old Hickory Lake, TN', '25 lb 0 oz')
('trout, cutthroat', 'O. Clarki', 'Pyramid Lake, NV', '41 lb 0 oz')
('perch, yellow', 'P. Flavescens', 'Bordentown, NJ', '4 lb 3 oz')
('bluegill', 'L. Macrochirus', 'Ketona Lake, AL', '4 lb 12 oz')
('gar, longnose', 'L. Osseus', 'Trinity River, TX', '50 lb 5 oz')
('crappie, white', 'P. annularis', 'Enid Dam, MS', '5 lb 3 oz')
('pickerel, grass', 'E. americanus', 'Dewart Lake, IN', '1 lb 0 oz')
('goldfish', 'C. auratus', 'Lake Hodges, CA', '6 lb 10 oz')
('salmon, chinook', 'O. Tshawytscha', 'Kenai River, AK', '97 lb 4 oz')
ニュージャージー州(NZ)
('perch, yellow', 'P. Flavescens', 'Bordentown, NJ', '4 lb 3 oz')
('first_name', 'last_name', 'common', 'location', 'state', 'weight', 'date')
fish_records
('George', 'Perry', 'bass, largemouth', 'Montgomery Lake', 'GA', '22 lb 4 oz', '1932-06-02')
('Mabry', 'Harper', 'walleye', 'Old Hickory Lake', 'TN', '25 lb 0 oz', '1960-08-02')
('John', 'Skimmerhorn', 'trout, cutthroat', 'Pyramid Lake', 'NV', '41 lb 0 oz', '1925-12-1')
('C.C.', 'Abbot', 'perch, yellow', 'Bordentown', 'NJ', '4 lb 3 oz', '1865-5-1')
('T.S.', 'Hudson', 'bluegill', 'Ketona Lake', 'AL', '4 lb 12 oz', '1950-4-9')
('Townsend', 'Miller', 'gar, longnose', 'Trinity River', 'TX', '50 lb 5 oz', '1954-7-30')
('Fred', 'Bright', 'crappie, white', 'Enid Dam', 'MS', '5 lb 3 oz', '1957-7-31')
('Mike', 'Berg', 'pickerel, grass', 'Dewart Lake', 'IN', '1 lb 0 oz', '1990-6-9')
('Florentino', 'Abena', 'goldfish', 'Lake Hodges', 'CA', '6 lb 10 oz', '1996-4-17')
('Les', 'Anderson', 'salmon, chinook', 'Kenai River', 'AK', '97 lb 4 oz', '1985-5-17')
ニュージャージー州(NZ)
('C.C.', 'Abbot', 'perch, yellow', 'Bordentown', 'NJ', '4 lb 3 oz', '1865-5-1')
$
0 コメント:
コメントを投稿