Python MySQLdb 编程错误:1064 插入数据时出现

3 投票
2 回答
2957 浏览
提问于 2025-04-17 21:07

我有这样一个列表:

info=[[u' Rasta.eon 2 - 1 Rasta.Xd   ', u'Razer CS:GO Tournament 2', u'26-02-2014'], [u' XPC 1       - 2 WP.GG  ', u'Roccat DotA 2 Tournament', u'26-02-2014']]

conn= MySQLdb.connect(host='localhost',user='root',passwd='',db='ee')

c = conn.cursor()
query = "INSERT INTO todaysmatches (match,tournamentname,matchdate) VALUES (%s,%s,%s)"
c.executemany(query, info)
conn.commit()
conn.close()

当我尝试执行这个查询时,出现了这个错误:

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 'match,tournamentname,matchdate) VALUES \n(' Rasta.eon 2 - 1 Rasta.Xd   ','Razer C' at line 1")

其中,match 是一个长度为150的字符串,tournamentname 也是一个长度为150的字符串,matchdate 是一个日期类型。

2 个回答

0

把你的第四行改成:

query = "INSERT INTO todaysmatches (`match`,`tournamentname`,`matchdate`) VALUES (%s,%s,%s)"
3

match 是 MySQL 中的一个关键字。如果你想把它当作列名使用,可以用反引号把它括起来。

INSERT INTO todaysmatches (`match`,tournamentname,matchdate) VALUES (%s,%s,%s)

不过,如果你给这个列起个其他名字会更方便。


看看当你尝试创建一个名为 match 的列时会发生什么:

mysql> create table foo (match varchar(150), tournamentname varchar(150), matchdate DATE);
ERROR 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 'match varchar(150), tournamentname varchar(150), matchdate DATE)' at line 1
mysql> create table foo (`match` varchar(150), tournamentname varchar(150), matchdate DATE);
Query OK, 0 rows affected (0.03 sec)

撰写回答