2

I am getting the following error:

query=query%db.literal(args)
ValueError: Unsupported Format character 'P' (0x50)

Here is my query being executed from python (note that phraseList is a list)

for elem in phraseList:
 cursor.execute("""SELECT PHRASE,COUNT(CASEID) FROM TEST.NER WHERE LABEL LIKE '%PART%' \
 AND CASEID IN (SELECT DISTINCT CASEID FROM TEST.NER WHERE LABEL LIKE '%CONDITION%'\
 AND PHRASE LIKEPHRASE LIKE %s""",(elem,))
asked May 8, 2013 at 8:10

2 Answers 2

2

MySQLdb overloads the Python string formatting syntax, and the %P part of %PART% is seen as a string formatting expression.

To prevent this you need to double the character to %%:

for elem in phraseList:
 cursor.execute("""SELECT PHRASE,COUNT(CASEID) FROM TEST.NER WHERE LABEL LIKE '%%PART%%' \
 AND CASEID IN (SELECT DISTINCT CASEID FROM TEST.NER WHERE LABEL LIKE '%%CONDITION%%'\
 AND PHRASE LIKEPHRASE LIKE %s""",(elem,))
answered May 8, 2013 at 8:33
Sign up to request clarification or add additional context in comments.

Comments

0

Here is what made the error disappear:

for elem in phraseList:
 part='%part%'
 condition='%condition%'
 query="SELECT PHRASE,COUNT(CASEID) FROM TEST.NER WHERE LABEL LIKE %s \
 AND CASEID IN (SELECT DISTINCT CASEID FROM TEST.NER WHERE LABEL LIKE %s \
 AND PHRASE LIKE %s)"
 params=(part,condition,elem)
 cursor.execute(query,params)
answered May 8, 2013 at 8:14

3 Comments

Now part and condition are interpolated without % wildcards and have been lowercased. Although using them as SQL parameters would help here, that is not going to work if you change the meaning.
@Martjin: Actually now it works even with part='%part%' as above
You still are matching against lowercase text instead of uppercase. The meaning is different from the original.

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.