【发布时间】:2013-05-04 13:26:09
【问题描述】:
在 MySql db 中有 DATETIME 类型的列。我需要按此列选择记录 - 小于当前 est 日期的记录。我使用来自Python - datetime of a specific timezone 的代码来确定预计日期:
import MySQLdb
import datetime
class EST(datetime.tzinfo):
def utcoffset(self, dt):
return datetime.timedelta()
def dst(self, dt):
return datetime.timedelta(0)
def get_est():
return datetime.datetime.now(EST())
这是一个选择请求:
a = 'SELECT * FROM Table1 WHERE Date <= %s' % get_est()
print a
cursor.execute(a)
data = cursor.fetchall()
for item in data:
print item
此时,我有一个错误:
File "/usr/lib/python2.7/dist-packages/MySQLdb/connections.py", line 36, in defaulterrorhandler
raise errorclass, errorvalue
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '= 2013-05-04 13:23:21.776521+00:00' at line 1")
我该如何摆脱它?请注意,我必须使用当前的 est 日期时间并将其传递给 sql 请求。
select * FROM Table1 WHERE Date <= '2013-05-04 13:34:03.461407+00:00'
【问题讨论】:
标签: python mysql python-2.7