Python - SQL - Databases: Getting Organized - insert, select, delete
Head First Python: A Learner’s Guide to the Fundamentals of Python Programming, A Brain-Friendly Guide、 Paul Barry(著)、 O’Reilly Mediaの Chapter 12.(Databases: Getting Organized)、EXERCISE(549/682)の解答を求めてみる。
Jupyter(コード、入出力結果)
webapp/PopulateTables.ipynb
import DBcm
db_details = 'CoachDB.sqlit3'
SQL_INSERT = '''
insert into swimmers (name, age)
values (?, ?)
'''
with DBcm.UseDatabase(db_details) as db:
db.execute(SQL_INSERT, (name, age,))
SQL = '''
select * from swimmers
'''
with DBcm.UseDatabase(db_details) as db:
db.execute(SQL)
results = db.fetchall()
results
[]
SQL = '''
delete from swimmers
'''
with DBcm.UseDatabase(db_details) as db:
db.execute(SQL)