代码之家  ›  专栏  ›  技术社区  ›  00__00__00

使用psycopg2删除名称中带引号的postgreSQL表

  •  0
  • 00__00__00  · 技术社区  · 4 年前

    我在模式中有一个表。

    my_schema.my_table
    

    在相同的模式中,我还有一个名为相同但带有引号的表

    my_schema."my_table"
    

    我怎么能放弃 my_schema."my_table" 使用psycopg2? (不冒摔下去的风险 my_schema.my_table )

    postgresConnection = psycopg2.connect(...)
    from psycopg2 import sql 
    
    cursor                = postgresConnection.cursor()
    name_Table            = 'my_schema."my_table"'
    cursor                = postgresConnection.cursor()
    dropTableStmt   = "drop TABLE %s;"%name_Table;
    cursor.execute(dropTableStmt)
    postgresConnection.commit()
    cursor.close();
    

    但我明白了

    SyntaxError: zero-length delimited identifier at or near """"
    LINE 1: drop TABLE my_schema.""my_table"";
    

    我也尝试过:

    from psycopg2 import sql 
    
    cursor                = postgresConnection.cursor()
    name_Table            = 'my_schema.""my_table""'
    cur = postgresConnection.cursor()
    cur.execute(sql.SQL("DROP table {table}").format(table=sql.Identifier(name_Table)))
    postgresConnection.commit()
    cursor.close();
    

    但我得到:

    UndefinedTable: table "my_schema.""my_table""" does not exist
    
    0 回复  |  直到 4 年前
        1
  •  2
  •   Laurenz Albe    4 年前

    要在对象名称中转义双引号,请将双引号加倍:

    DROP TABLE my_schema."""my_table""";