0

I am aware this may be a duplicate post. However I have looked at the other posts and cant figure it out in my case.

from configdata import configdata

from dbfiles.dbconnect import connection 

c,conn = connection()

table = configdata()[4]
userid = 'jdeepee'

value = c.execute("SELECT * FROM %s WHERE userid = (%s)" % (table), userid)

print(value)

I think the code is self explanatory. But essentially what I am trying to do is query a MySQL database based on a variable for the integer and userid. I believe my syntax is wrong not sure how to fix it however. Help would be great.

Joshua
  • 2,979
  • 1
  • 14
  • 20
  • 1
    this may help http://stackoverflow.com/questions/15255694/python-mysqldb-execute-table-variable – Vamsi Prabhala Dec 07 '15 at 21:05
  • I have tried to do that, I think it is the user id causing some issues. I get the error: TypeError: not enough arguments for format string – Joshua Dec 07 '15 at 21:17
  • I am curious. Why would you have code which reads a row from any table? If table is unknown in some part of code, the resulting row data will also contain unknown columns. How do you use that? – zvone Dec 07 '15 at 22:08

1 Answers1

1

Try this:

value = c.execute("SELECT * FROM {} WHERE userid = %s".format(table), (userid,))

Basically, you need to interpolate the table name into the query first, then pass any query parameters to .execute() in a tuple.

Eugene Yarmash
  • 142,882
  • 41
  • 325
  • 378