我正在使用 SqlAlchemy 使用手工编写的 SQL 从 PG 数据库中获取数据。我正在尝试一个包含类似 SQL 运算符 '%' 的查询,这似乎会导致 SqlAlcjhemy 陷入循环:
sql = """
SELECT DISTINCT u.name from user u
INNER JOIN city c ON u.city_id = c.id
WHERE c.designation=upper('fantasy')
AND c.id IN (select id from ref_geog where short_name LIKE '%opt')
"""
# The last line in the above statement throws the error mentioned in the title.
# However if the last line is change to:
# AND c.id IN (select id from ref_geog where short_name = 'helloopt')
# the script runs correctly.
#
# I also tried double escaping the '%' i.e. using '%%' instead - that generated the same error as previously.
connectDb()
res = executeSql(sql)
print res
closeDbConnection()
任何人都知道是什么导致了这一误导性错误消息以及我如何解决它?
[[Edit]]
在有人问之前,上面包含的功能并没有什么特别或奇特的地方。例如,函数executeSql() 只是调用conn.execute(sql) 并返回结果。变量 conn 只是之前建立的数据库连接。
你必须给予%%
将其用作%
因为%
在 python 中用作字符串格式,因此当您编写单个%
它假设您将用它替换一些值。
所以当你想放置单身时%
在带有查询的字符串中总是放置双精度%
.
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)