Python psycopg2转义字符

Python psycopg2转义字符,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我有一个postgres查询,如下所示,并在psql上运行find: select gcaseid, count (*) as N, array_agg(distinct nvchquestion) as questions, array_agg(nvchanswer) as answers from case_script, case_script_answer w

我有一个postgres查询,如下所示,并在psql上运行find:

select 
    gcaseid,
    count (*)                          as N,
    array_agg(distinct nvchquestion)   as questions,
    array_agg(nvchanswer)              as answers
from
    case_script,
    case_script_answer
where
    case_script.gscriptid = case_script_answer.gscriptid and
    nvchquestion ilike '%blood pressure%'
group by
    gcaseid
order by
    N desc
;
现在,我想用Python实现一个类似的查询,这就是我想到的:

import psycopg2

def getAnswers(question):

    query = '''
            select 
                gcaseid,
                count (*)                          as N,
                array_agg(distinct nvchquestion)   as questions,
                array_agg(nvchanswer)              as answers
            from
                case_script,
                case_script_answer
            where
                case_script.gscriptid = case_script_answer.gscriptid and
                nvchquestion ilike %s
            group by
                gcaseid
            order by
                N desc
            ;
    '''%(r"'%%s%'")

    conn = psycopg2.connect("dbname='sos' user='postgres' host='localhost'")
    cur  = conn.cursor()

    cur.mogrify(query, (question,))
    # cur.execute(query, (question,))

    # result = cur.fetchall()

    cur.close()
    conn.close()

    return result

if __name__ == '__main__':

    print getAnswers('blood pressure')
    print 'done'
现在,当我运行此查询时,我得到了错误:

$ python updateTable.py
Traceback (most recent call last):
  File "updateTable.py", line 39, in <module>
    print getAnswers('blood pressure')
  File "updateTable.py", line 27, in getAnswers
    cur.mogrify(query, (question,))
ValueError: unsupported format character ''' (0x27) at index 442
$python updateTable.py
回溯(最近一次呼叫最后一次):
文件“updateTable.py”,第39行,在
打印getAnswers(“血压”)
getAnswers中第27行的文件“updateTable.py”
cur.mogrify(查询,(问题)
ValueError:索引442处不支持的格式字符“”(0x27)

不知道发生了什么。任何人都可以澄清吗?

请在查询中使用
%%
来表示
类似的
野生字符:

execute(" ... ilike %%%s%%", [question])
或者在您的值中用
%
s环绕您的值:

execute(" ... ilike %s", ['%' + question + '%']

请参阅查询中的。

使用
%%
来表示类似于

execute(" ... ilike %%%s%%", [question])
或者在您的值中用
%
s环绕您的值:

execute(" ... ilike %s", ['%' + question + '%']

请参阅。

传递连接到参数的
%
的最简单方法,建议如下:

输出:

select 'x' ilike '%x%'
True
select 'x' ilike format('%%%1$s%%', 'x')
True
但是,如果要保持参数干净,请使用
格式

query = "select 'x' ilike format('%%%%%%1$s%%%%', %s)"
print (cursor.mogrify(query, ('x',)).decode('utf8'))
cursor.execute(query, ('x',))
print (cursor.fetchone()[0])
输出:

select 'x' ilike '%x%'
True
select 'x' ilike format('%%%1$s%%', 'x')
True

最简单的方法是按照以下建议将连接到参数的
%
传递到参数:

输出:

select 'x' ilike '%x%'
True
select 'x' ilike format('%%%1$s%%', 'x')
True
但是,如果要保持参数干净,请使用
格式

query = "select 'x' ilike format('%%%%%%1$s%%%%', %s)"
print (cursor.mogrify(query, ('x',)).decode('utf8'))
cursor.execute(query, ('x',))
print (cursor.fetchone()[0])
输出:

select 'x' ilike '%x%'
True
select 'x' ilike format('%%%1$s%%', 'x')
True

谢谢这就是我最后使用的!谢谢这就是我最后使用的!