PythonのSQLiteモジュールを使ってdate型やdatetime型を扱う方法
テーブルの作成
$ sqlite3 data.db
sqlite> CREATE TABLE sample(d date, t timestamp);
sqlite> .quit
Pythonでdate型やdatetime型の読み書き
>>> import sqlite3, datetime, time
>>> con = sqlite3.connect('data.db', detect_types = sqlite3.PARSE_DECLTYPES|sqlite3.PARSE_COLNAMES)
データの追加
>>> today = datetime.date.today()
>>> now = datetime.datetime.now()
>>> con.execute('INSERT INTO sample(d, t) values (?,?)', (today, now))
データの読み出し
>>> con.execute('SELECT * FROM sample').fetchall()
[(datetime.date(2016, 5, 17), datetime.datetime(2016, 5, 17, 11, 45, 20, 153520))]