[英]psycopg2 table name as a parameter fails with double quotes
我使用这个 python function 从 postGres 中的不同表中获取列名。
def get_table_columns(conn, table):
"""
Retrieve a list of column names in a table
:param conn:
:param table:
:return: List of column names
"""
try:
query = sql.SQL('SELECT * FROM {} LIMIT 0').format(sql.Identifier(table))
print(query.as_string(conn))
with conn as c:
with c.cursor() as cur:
cur.execute(query)
return [desc[0] for desc in cur.description]
except psycopg2.DatabaseError as de:
logger.error("DatabaseError in get_table_columns: {0}".format(str(de)))
raise de
except Exception as ex:
logger.error("Exception in get_table_columns: {0}".format(str(ex)))
raise ex
我收到错误消息,“关系“v43fs.evt_event_cycle”不存在。
打印语句如下所示: SELECT * FROM "v43fs.evt_event_cycle" LIMIT 0
双引号导致查询失败。 我怎样才能让它们 go 离开?
我将查询更改为如下格式,现在效果更好:
query = sql.SQL('SELECT * FROM {}.{} LIMIT 0').format(sql.Identifier(schema), sql.Identifier(table))
谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.