Head First Programming
A learner's guide to programming
using the Python language
( O'Reilly Media; )
David Griffiths (著) Paul Barry (著)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python (プログラミング言語)
Head First Programming A learner's guide to programming using the Python language (David Griffiths(著)、Paul Barry(著)、 O'Reilly Media; )のChapter 5(hashes and databases - Putting data in its place)、Sharpen your pencil(p.171)を解いてみる。
Sharpen your pencil(p.171)
コード(BBEdit)
sample171.py
#!/usr/bin/env python3
#-*- coding: utf-8
import sqlite3 as dbapi
def findDetails(id2find):
db = dbapi.connect('surfersDB.sdb')
db.row_factory = dbapi.Row
cursor = db.cursor()
cursor.execute('select * from surfers')
rows = cursor.fetchall()
cursor.close()
for row in rows:
if row['id'] == id2find:
return {'id': str(row['id']), 'name':row['name'],
'country':row['country'], 'average':str(row['average']),
'board':row['board'], 'age':str(row['age'])}
return {}
if __name__ == '__main__':
for id2find in [100, 105, 110, 200]:
print('id: {0} {1}'.format(id2find, findDetails(id2find)))
入出力結果(Terminal)
$ ./sample171.py
id: 100 {}
id: 105 {'id': '105', 'name': "Aideen 'board babe' Wu", 'average': '8.65', 'board': 'Fish', 'age': '24', 'country': 'Japan'}
id: 110 {}
id: 200 {}
$
0 コメント:
コメントを投稿