_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax;)
_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax;)
我是 Python 的新用户 MySLQdb。我有这个代码:
for row in csv_reader:
insert = """INSERT INTO %s
VALUES (DEFAULT, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """
cursor.execute(insert, (nome_tabela, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
但是当我执行时,出现以下错误: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 ''Aquecimento'\n\t\t\t\t\tVALUES (DEFAULT, 'c00010', 'Dorm1', '0.0', '0.0', '0.0', '3.4' at line 1")
我想错误与 table 的名称有关,但我不确定。
Afaik,mysql-python
不处理 table 名称替换:它盲目地在所有变量周围添加引号并再次转义数据 SQL 注入。
你最好的运气是自己连接字符串,但在这种情况下你需要格外小心 name_tabela
的内容:
insert = (
"INSERT INTO %s" % name_tabela
+ " VALUES (DEFAULT, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
)
cursor.execute(insert, (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
顺便说一句,您可以这样简化 execute
第二个参数:
cursor.execute(insert, row[:15])
# or even this if the `row` has exactly 15 values
cursor.execute(insert, row)
我是 Python 的新用户 MySLQdb。我有这个代码:
for row in csv_reader:
insert = """INSERT INTO %s
VALUES (DEFAULT, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s) """
cursor.execute(insert, (nome_tabela, row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
但是当我执行时,出现以下错误: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 ''Aquecimento'\n\t\t\t\t\tVALUES (DEFAULT, 'c00010', 'Dorm1', '0.0', '0.0', '0.0', '3.4' at line 1")
我想错误与 table 的名称有关,但我不确定。
Afaik,mysql-python
不处理 table 名称替换:它盲目地在所有变量周围添加引号并再次转义数据 SQL 注入。
你最好的运气是自己连接字符串,但在这种情况下你需要格外小心 name_tabela
的内容:
insert = (
"INSERT INTO %s" % name_tabela
+ " VALUES (DEFAULT, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s)"
)
cursor.execute(insert, (row[0], row[1], row[2], row[3], row[4], row[5], row[6], row[7], row[8], row[9], row[10], row[11], row[12], row[13], row[14]))
顺便说一句,您可以这样简化 execute
第二个参数:
cursor.execute(insert, row[:15])
# or even this if the `row` has exactly 15 values
cursor.execute(insert, row)