簡體   English   中英

psycopg2 表名作為參數失敗並帶有雙引號

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM