2

我收到以下错误:

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

这是我从 python 执行的查询(注意,phraseList 是一个列表)

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

2 回答 2

2

MySQLdb 重载了 Python 字符串格式化语法,%P部分%PART%被视为字符串格式化表达式。

为防止这种情况,您需要将字符加倍为%%

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,))
于 2013-05-08T08:33:19.573 回答
0

这是使错误消失的原因:

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)
于 2013-05-08T08:14:49.863 回答