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.

3
  • 1
    this may help stackoverflow.com/questions/15255694/… Commented Dec 7, 2015 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 Commented Dec 7, 2015 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? Commented Dec 7, 2015 at 22:08

1 Answer 1

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.

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

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.