In python with MySQLdb I am trying to execute the following:
cur.execute("select COUNT(*) from adjacency where r = %d and c = %d)" % (curRow, curCol)
r and c are integer fields in table adjacency and also form a composite primary key. curRow and curCol are integer variables in my python program.
MySQL is complaining:
_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")
Ultimately, I want to check if there already exists a row in adjacency with r=curCow and c=curCol. If so, then update a field in the table. Otherwise insert a row into the table.
- What is wrong with my prepared statement?
All help is greatly appreciated!
-
1"What is wrong with my prepared statement?" YOu have no prepared statement. These are not supported by the Python MySQLdb module.glglgl– glglgl2014年12月05日 15:36:11 +00:00Commented Dec 5, 2014 at 15:36
1 Answer 1
Looks like you just have a bracket in the wrong place.
Change...
cur.execute("select COUNT(*) from adjacency where r = %d and c = %d)" % (curRow, curCol)
...to...
cur.execute("select COUNT(*) from adjacency where r = %d and c = %d" % (curRow, curCol))
...although it's safer to use...
cur.execute("select COUNT(*) from adjacency where r = %s and c = %s", (curRow, curCol))
...which will protect you from potential SQL injection.
Comments
Explore related questions
See similar questions with these tags.