【问题标题】:Passing est datetime to MySql将 est 日期时间传递给 MySql
【发布时间】: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


    【解决方案1】:

    您的日期值似乎不在引号中。

    a = "SELECT * FROM Table1 WHERE Date <= '%s'" % get_est()
    

    (我不懂python,所以如果我的语法不正确,我很抱歉。)

    【讨论】:

    • 您能发布最终查询的样子吗?
    猜你喜欢
    • 2017-10-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-24
    • 2011-07-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多