2012-02-28 13 views
2
import sqlite3 
import datetime 

today = datetime.date.today() 

db_name = "test.db" 
db = sqlite3.connect(db_name, detect_types=sqlite3.PARSE_DECLTYPES) 
c = db.cursor() 
c.execute('create table changes (type string, file_name string, modified timestamp)') 
c.execute('insert into changes values(?, ?, ?)', ('M', 'source/test-this.rst', datetime.datetime.now())) 
db.commit() 
last_run = datetime.datetime(2012, 2, 27, 23, 22, 21, 613477) 
c.execute('''select modified from changes where modified > ?''', (last_run)) 
print c.fetchall() 

...PythonでSQLiteを使用してdatetimeベースのクエリを実行するにはどうすればよいですか?私はこのエラーを取得してい

$ python sqlite.py 
Traceback (most recent call last): 
    File "sqlite.py", line 18, in <module> 
    c.execute('''select modified from changes where modified > ?''', (last_run)) 
ValueError: parameters are of unsupported type 

答えて

3

は、単にタプルを使用します。

c.execute('''select modified from changes where modified > ?''', (last_run,)) 

これは働いていた共通の落とし穴...

+0

です。ありがとう! – espeed

+0

タプル()もあります – TeamBlast

関連する問題