在 psycopg2 中插入查询值时删除引号
removing quotes when interpolating query value in psycopg2
连接到 ,但我遗漏了一些东西。
当我运行以下
table_name = 'my_schema.my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM {}').format(sql.Identifier(table_name)))
发送到数据库的查询是
SELECT col1, col2 FROM "myschema.myname"
我收到错误:
"relation "myschema.myname" does not exist"
我希望查询是
SELECT col1, col2 FROM myschema.myname
当我将其直接传递给 cur.execute
时,我没有问题。
如果有帮助,我在 this tutorial 之后使用 .ini 文件连接到数据库,在我的例子中如下所示:
[postgresql]
host=ip_no_of_host
database=name_of_db
user=username
password=password
schema_name = 'my_schema'
table_name = 'my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM {}.{}').format(
sql.Identifier(schema_name), sql.Identifier(table_name)
)
)
或者只是
table_name = 'my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM my_schema.{}').format(
sql.Identifier(table_name)
)
)
连接到
当我运行以下
table_name = 'my_schema.my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM {}').format(sql.Identifier(table_name)))
发送到数据库的查询是
SELECT col1, col2 FROM "myschema.myname"
我收到错误:
"relation "myschema.myname" does not exist"
我希望查询是
SELECT col1, col2 FROM myschema.myname
当我将其直接传递给 cur.execute
时,我没有问题。
如果有帮助,我在 this tutorial 之后使用 .ini 文件连接到数据库,在我的例子中如下所示:
[postgresql]
host=ip_no_of_host
database=name_of_db
user=username
password=password
schema_name = 'my_schema'
table_name = 'my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM {}.{}').format(
sql.Identifier(schema_name), sql.Identifier(table_name)
)
)
或者只是
table_name = 'my_table'
cur.execute(sql.SQL('SELECT col1, col2 FROM my_schema.{}').format(
sql.Identifier(table_name)
)
)