0

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.

  1. What is wrong with my prepared statement?

All help is greatly appreciated!

asked Jun 22, 2013 at 20:47
1
  • 1
    "What is wrong with my prepared statement?" YOu have no prepared statement. These are not supported by the Python MySQLdb module. Commented Dec 5, 2014 at 15:36

1 Answer 1

6

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.

answered Jun 22, 2013 at 20:50
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.