我使用手工制作的 SQL 从 PG 数据库中获取数据,使用的是 SqlAlchemy。我正在尝试一个查询,其中包含 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()
任何人都知道是什么导致这个误导性错误消息,我可以如何修复它?
[编辑]
在任何人询问之前,上面所包含的函数没有什么特别或奇特的地方。例如,函数 ExecuteSql ()只调用 conn.execute (sql)并返回结果。变量 con 只是以前建立的到数据库的连接。