【发布时间】:2021-08-17 22:28:11
【问题描述】:
我正在尝试编写以下代码
import psycopg2
param_dic = {
"host" : "localhost",
"database" : "test",
"user" : "test",
"password" : "****"
}
conn = psycopg2.connect(**param_dic)
cursor = conn.cursor()
cursor.execute('select * from test where code in ("0091","0092","0FY00Z0","0FY00Z1","0FY00Z2","5051","5059")')
在上面的cursor execute 命令中,无论我使用single quote 还是double quotes 我都会得到如下所示的错误
ndefinedColumn Traceback(最近调用 最后)在 ----> 1 cursor.execute('select * from test where code in ("0091","0092","0FY00Z0","0FY00Z1","0FY00Z2","5051","5059")')
UndefinedColumn:列“0091”不存在第 1 行:...来自 测试代码在哪里 ("0091","00...
当我将引号更改为 single quote 时,出现以下错误
cursor.execute('select * from test where code in ('0091','0092','0FY00Z0','0FY00Z1','0FY00Z2','5051','5059')') ^ SyntaxError: 无效标记
当我尝试使用ilike 运算符时,我收到以下错误
cursor.execute ('select code from test where long_title ilike '%live%' and long_title ilike '%ransplan%'')
NameError: name 'live' 没有定义
但上述所有查询在我的 pgadmin postgresql 编辑器中都可以正常工作。因此,原生形式的 SQL 没有问题,但是当我尝试使用 cursor.execute 在 python 中使用它们时,我遇到了问题。
你能帮我解决这个问题吗?
【问题讨论】:
标签: python sql postgresql psycopg2 quoting