首页 > 解决方案 > 连接到 MySQL 时出错 1064 (42000) 尝试插入时

问题描述

我是新来的,对 Mysql 和 Python 非常陌生。我对这个插入语句有疑问:

query = "INSERT INTO {0} (date, open, high, low, close, volume, ivol, hvol) VALUES(%s,%s,%s,%s,%s,%s,%s,%s))".format(str.lower(contract.symbol))  

dd = [(20210325, 34.0, 34.03, 33.91, 33.96, 500850, 0, 0), (20210326, 34.57, 34.69, 34.14, 34.53, 480315, 0, 0)]

cursor.executemany(query, dd)

错误是:

Error while connecting to MySQL 1064 (42000): 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 '),(20210326,34.57,34.69,34.14,34.53,480315,0,0))' at line 1

该表是使用以下语句创建的:

createquery = ("CREATE TABLE {0}( date INT(8) UNSIGNED PRIMARY KEY NOT NULL, open DECIMAL(18,4) NOT NULL, high DECIMAL(18, 4) NOT NULL, low DECIMAL(18,4) NOT NULL, close DECIMAL(18,4) NOT NULL, volume BIGINT NOT NULL, ivol DECIMAL(18,4) NOT NULL,hvol DECIMAL(18,4) NOT NULL)").format(str.lower(contract.symbol)) 

我花了一整天的时间试图弄清楚这一点。谁能给我指点?

标签: pythonmysql

解决方案


你的INSERT陈述最后有一个太多)

query = "INSERT INTO {0} (date, open, high, low, close, volume, ivol, hvol) VALUES(%s,%s,%s,%s,%s,%s,%s,%s))".format(str.lower(contract.symbol))

应该:

query = "INSERT INTO {0} (date, open, high, low, close, volume, ivol, hvol) VALUES(%s,%s,%s,%s,%s,%s,%s,%s)".format(str.lower(contract.symbol))

推荐阅读