0

I'm trying to bind a parameter in SQL:

sql = "SELECT COLUMN_NAME,DATA_TYPE FROM USER_TAB_COLS WHERE TABLE_NAME=UPPER(:TABLENAME)"
print 'TABLENAME=',TABLENAME
sqlqry = sql %(TABLENAME)

but get the string formatting error:

TypeError: not all arguments converted during string formatting

What could be the problem?

4
  • 3
    What did you expect? I don't see any valid format specifiers in your string. Commented Nov 14, 2012 at 21:31
  • 1
    If you're using sqlite3 I'd recommend using prepared statements. Commented Nov 14, 2012 at 21:33
  • I thought the % will substitute the TABLENAME value Commented Nov 14, 2012 at 21:33
  • If you want to use a keyword in your substitution, replace :TABLENAME with %(TABLENAME)s, and then substitute like sql % {'TABLENAME': TABLENAME}. If you don't care about keywords, just use %s as mentioned below (however this introduces SQL-injection risks and prepared statements as mentioned by @Whymarrh would be the safest). Commented Nov 14, 2012 at 21:36

2 Answers 2

1

You are missing a placeholder in your string.

You want to replace :TABLENAME in the string sql with %s I suspect.

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

Comments

0

You really should post a bit more code for this to be entirely clear, but it looks like the string 'sql' doesn't contain any %-codes to be substituded. You are trying to substitude one value, but not all of those were used (in other words, nothing was substituted). You should use %s instead of :TABLENAME.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.