Using python and postgres, variables within execute function?

后端 未结 2 1608
野的像风
野的像风 2020-12-18 14:22

I had a question regarding the usage of variables inside a python function which accesses the PostgreSQL server. For example, the following:

def delete():
           


        
2条回答
  •  离开以前
    2020-12-18 15:09

    To pass identifiers use psycopg2.extensions.AsIs

    from psycopg2.extensions import AsIs
    
    def update(table_name, var_1, var_2):
        cur.execute("""
            UPDATE %s
            SET %s = 'Y'
            WHERE %s = 'John';
            """,
            (AsIs(table_name), AsIs(var_1), AsIs(var_2))
        )
    

提交回复
热议问题