0

在带有 MySQLdb 的 python 中,我试图执行以下操作:

cur.execute("select COUNT(*) from adjacency where r = %d and c = %d)" % (curRow, curCol) 

rc是表中的整数字段,adjacency也构成复合主键。curRow并且curCol是我的python程序中的整数变量。MySQL 抱怨:

_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 '%d and c = %d' at line 1") 

最终,我想检查是否已经存在与r=curCowand相邻的行c=curCol。如果是这样,则更新表中的字段。否则在表中插入一行。

  1. 我准备好的陈述有什么问题?

非常感谢所有帮助!

4

1 回答 1

6

看起来你只是在错误的地方放了一个支架。

改变...

cur.execute("select COUNT(*) from adjacency where r = %d and c = %d)" % (curRow, curCol)

...至...

cur.execute("select COUNT(*) from adjacency where r = %d and c = %d" % (curRow, curCol))

...虽然使用起来更安全...

cur.execute("select COUNT(*) from adjacency where r = %s and c = %s", (curRow, curCol))

...这将保护您免受潜在的 SQL 注入。

于 2013-06-22T20:50:28.327 回答