将est datetime传递给MySql
问题描述:
在MySql db中有DATETIME类型的列。我需要选择这一列的记录 - 那些小于当前日期的记录。我使用的代码从Python - datetime of a specific timezone确定销售日期:将est datetime传递给MySql
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'
答
它看起来像你的日期值不是引号。
a = "SELECT * FROM Table1 WHERE Date <= '%s'" % get_est()
(我不知道蟒蛇,所以我很抱歉,如果我的语法不正确。)
没有帮助.... – 2013-05-04 13:34:38
您可以发布您最终的查询是什么样子? – 2013-05-04 13:35:39
我只是做了.. – 2013-05-04 13:38:56