Python和MySQL STR_TO_DATE

时间:2012-07-02 12:42:32

标签: python mysql

当我执行代码(见下文)时,我收到了下一个错误

Traceback (most recent call last):
File "/home/test/python/main.py", line 18, in <module>
  calls = tel.getCallRecordings(member['id'], member['start_date'])
File "/home/test/python/teldb.py", line 49, in getCallRecordings
  'start_date': str(start_date),
File "/usr/local/lib/python2.5/site-packages/MySQL_python-1.2.3-py2.5-freebsd-8.1-RELEASE-amd64.egg/MySQLdb/cursors.py", line 159, in execute
TypeError: int argument required

我使用的代码是下一个:

def getCallRecordings(self, member_id, start_date):       
    self.cursor.execute("""
    SELECT var10 as filename,
           var9 as duration
    FROM csv_data c
    LEFT JOIN transcriptions t ON
        c.id=t.call_id
    WHERE member_id=%(member_id)s AND
          var10 IS NOT NULL AND
          var9>%(min_duration)d AND
          dialed_date>STR_TO_DATE(%(start_date)s, '%%Y-%%m-%%d %%H:%%i:%%s') AND
          t.call_id IS NULL
    ORDER BY dialed_date DESC
    """, {
        'member_id': member_id,
        'min_duration': MIN_DURATION,
        'start_date': str(start_date),
        })
    logging.debug("Executed query: %s" % self.cursor._executed)
    return self.cursor.fetchone()

为什么我收到此错误?感谢。

1 个答案:

答案 0 :(得分:1)

您没有显示MIN_DURATION是什么,但是您使用%(min_duration)d,因此它必须是int。你有把它定义为字符串或浮点数吗?

您不需要在这样的SQL查询中使用%d,数据库适配器会理解您传递它的类型,并将它们正确地插入到查询中。使MIN_DURATION成为一个整数(可能在您拥有的代码中使用int(MIN_DURATION)),它将起作用。

顺便说一句:堆栈跟踪将您聚焦在str(start_date)上,但这只是因为那是可执行语句中有错误的最后一行,所以它会产生误导。