【问题标题】:Python MySQL Error 1064 inserting datetime stringPython MySQL 错误 1064 插入日期时间字符串
【发布时间】:2014-12-22 23:20:42
【问题描述】:

我有以下 python 代码:

now = time.strftime('%Y-%m-%d %H:%M:%S')
#now = datetime.datetime.now()
query = """INSERT INTO bandwidth_by_second (current_time, down, up)  VALUES (%s, %s, %s)"""
data = (now, 1.0, 2.0)
cursor.execute(query, data)

我的这个表的架构是:

  • current_time - 数据时间
  • 向下 - 双
  • 向上 - 双倍

运行时出现以下错误:

_mysql_exceptions.ProgrammingError: (1064, "您的 SQL 语法有错误;请查看与您的 MySQL 服务器对应的手册 在 'current_time, down, up) 附近使用正确语法的版本 VALUES ('2014-10-27 18:29:32', 1, 1)' at line 1")

我原以为我的日期时间格式错误,但this post 建议不正确。

这到底是怎么回事?

【问题讨论】:

    标签: python mysql datetime


    【解决方案1】:

    在引号中使用“%s”。然后它将起作用:)

    【讨论】:

    • 我看到其他答案暗示了同样的事情。当我这样做query = """INSERT INTO bandwidth_by_second (current_time, down, up) VALUES ('%s', %s, %s)""" 时,产生的错误看起来像(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 'current_time, down, up) VALUES (''2014-10-28 08:11:52.306849'', 1, 1)' at line 1")
    【解决方案2】:

    这与数据库表中的列名有关。我将current_time更改为currenttime,它开始工作了。

    【讨论】:

    • 你是我的神。我只是花了几个小时试图弄清楚这一点。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-10-09
    • 2013-07-26
    • 1970-01-01
    • 2013-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多