0
我想在我的查詢使用Psycopg2一個Postgres數據庫使用Python datetime對象,但我有以下錯誤日期時間語法錯誤:與Psycopg2
代碼:
# createdat is a datetime variable I used before
print(type(createdat))
q = """
select created_at
from mytable t
and t.created_at < %(createdat)s
""" % {
'createdat' : createdat
}
read.execute(q)
返回:
<type 'datetime.datetime'>
Traceback (most recent call last):
File "import.py", line 346, in <module>
read.execute(q)
File "/usr/lib/python2.7/dist-packages/psycopg2/extras.py", line 120, in execute
return super(DictCursor, self).execute(query, vars)
psycopg2.ProgrammingError: syntax error at or near "20"
LINE 6: and created_at < 2015-05-05 20:22:22
任何想法?在此先感謝
我不知道你的'createdat'來源於但你不應該自己進行注射參數。 [讓psycopg2自己處理參數](http://initd.org/psycopg/docs/usage.html#passing-parameters-to-sql-queries)。 (另外它更有可能解決你的問題。) –