0

My code:

selectQ ="""SELECT * FROM  %s WHERE %s = %%s order by RAND() limit %s""" % (self.table,self.columnSpecName,limit),
self.db.execute(str(selectQ),(idKey))

I get this exception: 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 ''SELECT * FROM table WHERE person_oid = 16 order by RAND() limit 10',)' at line 1")

However if I copy the query and run it through mysql it runs just fine.

What am I overlooking ?

1
  • It substitutes %%s with %s so it can be used in db.execute which will substitute it with idKey Commented Feb 22, 2012 at 12:46

2 Answers 2

3

You should look closer to:

syntax to use near ''SELECT * FROM table WHERE person_oid = 16 order by RAND() limit 10',)' at line 1")

And you will see double apostrophe at the beginning of the query and ,)' at the end of it.

selectQ ="""SELECT * FROM  %s WHERE %s = %%s order by RAND() limit %s""" % (self.table,self.columnSpecName,limit),
self.db.execute(str(selectQ),(idKey))

You've got a comma at the end of the first line, making it a tuple

Remove it and you will not need str(selectQ) on the second line.

Sign up to request clarification or add additional context in comments.

Comments

2

You have a comma to much in the first line (at the end):

selectQ ="""SELECT * FROM  %s WHERE %s = %%s order by RAND() limit %s""" % (self.table,self.columnSpecName,limit),
self.db.execute(str(selectQ),(idKey))

Change it to:

selectQ ="""SELECT * FROM  %s WHERE %s = %%s order by RAND() limit %s""" % (self.table,self.columnSpecName,limit)
self.db.execute(str(selectQ),(idKey))

Comments

Your Answer

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.