Skip to content Skip to sidebar Skip to footer

Python Sql Valueerror

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 phraseLi

Solution 1:

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,))

Solution 2:

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)

Post a Comment for "Python Sql Valueerror"