2014-10-27 54 views
0

我有以下Python代码:Python的MySQL错误1064插入的时间字符串

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 - DATATIME
  • 下来 - 双
  • 了 - double

When运行此,我得到以下错误:

_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 'current_time, down, up) VALUES ('2014-10-27 18:29:32', 1, 1)' at line 1")

我曾以为我已经格式化的日期时间不对,但this post并非如此。

这是怎么回事?

回答

0

它必须与数据库表中的列名称。我将current_time更改为currenttime并开始工作。

2

在引号中使用'%s'。它会工作:)

+0

我看到其他答案表明同样的事情。当我这样做'query =“”“INSERT INTO bandwidth_by_second(current_time,down,up)VALUES('%s',%s,%s)”“”',结果的错误看起来像'(1064,“You have在你的SQL语法中出错;查看与你的MySQL服务器版本相对应的手册,以在'current_time,down,up'附近使用正确的语法)VALUES(''2014-10-28 08:11:52.306849'', 1)'在第1行“) – 2014-10-28 07:13:06