在 pg8000 中使用 % 通配符
Using % wildcard with pg8000
我有一个类似于下面的查询:
def connection():
pcon = pg8000.connect(host='host', port=1234, user='user', password='password', database = 'database')
return pcon, pcon.cursor()
pcon, pcur = connection()
query = """ SELECT * FROM db WHERE (db.foo LIKE 'string-%' OR db.foo LIKE 'bar-%')"""
db = pd.read_sql_query(query, pcon)
然而,当我尝试 运行 我得到的代码时:
DatabaseError: '%'' not supported in a quoted string within the query string
我试过使用 \ 和额外的 % 转义符号,但没有成功。我怎样才能让 pg8000 正确地将其视为通配符?
"In Python, %
usually refers to a variable that follows the string. If you want a literal percent sign, then you need to double it. %%
"
-- Source
LIKE 'string-%%'
否则,如果这不起作用,PostgreSQL 也支持 underscores for pattern matching。
'abc' LIKE 'abc' true
'abc' LIKE 'a%' true
'abc' LIKE '_b_' true
但是,正如评论中提到的,
An underscore (_) in pattern
stands for (matches) any single character; a percent sign (%) matches any sequence of zero or more characters
不过,根据 source code,问题似乎是 LIKE
语句中 %
后面的单引号。
if next_c == "%":
in_param_escape = True
else:
raise InterfaceError(
"'%" + next_c + "' not supported in a quoted "
"string within the query string")
所以如果 next_c == "'"
而不是 next_c == "%"
,那么你会得到你的错误
'%'' not supported in a quoted string within the query string
使用最新版本的 pg8000,LIKE
中的 %
应该不会有任何问题。例如:
>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> cur.execute("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>> for title in ("Ender's Game", "The Magus"):
... cur.execute("INSERT INTO book (title) VALUES (%s)", [title])
>>>
>>> cur.execute("SELECT * from book WHERE title LIKE 'The %'")
>>> cur.fetchall()
([2, 'The Magus'],)
我有一个类似于下面的查询:
def connection():
pcon = pg8000.connect(host='host', port=1234, user='user', password='password', database = 'database')
return pcon, pcon.cursor()
pcon, pcur = connection()
query = """ SELECT * FROM db WHERE (db.foo LIKE 'string-%' OR db.foo LIKE 'bar-%')"""
db = pd.read_sql_query(query, pcon)
然而,当我尝试 运行 我得到的代码时:
DatabaseError: '%'' not supported in a quoted string within the query string
我试过使用 \ 和额外的 % 转义符号,但没有成功。我怎样才能让 pg8000 正确地将其视为通配符?
"In Python, %
usually refers to a variable that follows the string. If you want a literal percent sign, then you need to double it. %%
"
-- Source
LIKE 'string-%%'
否则,如果这不起作用,PostgreSQL 也支持 underscores for pattern matching。
'abc' LIKE 'abc' true
'abc' LIKE 'a%' true
'abc' LIKE '_b_' true
但是,正如评论中提到的,
An underscore (_) in
pattern
stands for (matches) any single character; a percent sign (%) matches any sequence of zero or more characters
不过,根据 source code,问题似乎是 LIKE
语句中 %
后面的单引号。
if next_c == "%":
in_param_escape = True
else:
raise InterfaceError(
"'%" + next_c + "' not supported in a quoted "
"string within the query string")
所以如果 next_c == "'"
而不是 next_c == "%"
,那么你会得到你的错误
'%'' not supported in a quoted string within the query string
使用最新版本的 pg8000,LIKE
中的 %
应该不会有任何问题。例如:
>>> import pg8000.dbapi
>>>
>>> con = pg8000.dbapi.connect(user="postgres", password="cpsnow")
>>> cur = con.cursor()
>>> cur.execute("CREATE TEMPORARY TABLE book (id SERIAL, title TEXT)")
>>> for title in ("Ender's Game", "The Magus"):
... cur.execute("INSERT INTO book (title) VALUES (%s)", [title])
>>>
>>> cur.execute("SELECT * from book WHERE title LIKE 'The %'")
>>> cur.fetchall()
([2, 'The Magus'],)