gpt4 book ai didi

python - 奇怪的 SQLAlchemy 错误消息 : TypeError: 'dict' object does not support indexing

转载 作者:IT老高 更新时间:2023-10-28 21:07:02 32 4
gpt4 key购买 nike

我正在使用手工制作的 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) 并返回结果。变量 conn 只是先前建立的与数据库的连接。

最佳答案

您必须给 %% 以将其用作 % 因为 % 在 python 中用作字符串格式,因此当您编写单个 % 它假设你要用这个替换一些值。

因此,当您想将单个 % 放置在带有查询的字符串中时,请始终放置双 %

关于python - 奇怪的 SQLAlchemy 错误消息 : TypeError: 'dict' object does not support indexing,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8657508/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com