2012年10月20日土曜日

python - sqlite3 を使う

connect  して cursor を作成してコマンドを実行する。
import sqlite3

conn = sqlite3.connect('sq.db')
cursor = conn.cursor()

cursor.execute('drop table people')

create_table_command = 'create table people (name char(30), age int(4))'

cursor.execute(create_table_command)

cursor.execute('insert into people values (?, ?)', ('Aoki',50))
cursor.executemany('insert into people values (?, ?)', [('Ishikawa', 70), ('Ueno', 40)])

conn.commit()

cursor.execute('select * from people')
print cursor.fetchall()

cursor.execute('select * from people where age >= 50')
print cursor.fetchall()

cursor.execute('update people set age=? where name = ?', (51, 'Aoki'))
cursor.execute('select * from people where age >= 50')
print cursor.fetchall()

0 件のコメント:

コメントを投稿