简体   繁体   中英

How to add escape with in python “”" quote string?

i am trying to integrate my AWS lambda function to query my postgresql.

sql_update_query = """update  "YP_SUPPLIERS" set %s =%s where "YP_SUPPLIERS"."supplierID"= %s"""
    cursor.execute(sql_update_query, (key[0],value[0],supplierID))

Apparently while creating the table "" where used. This is pretty hectic to work around this.

error

syntax error at or near "'supplierName'"
LINE 1: update  "YP_SUPPLIERS" set 'supplierName'='key' where "YP_SU...

it seems set %s who's value is supplierName should be with in "". Can any one let me know a work around or how can i implement this properly please

Use the SQL composition feature of psycopg2

from psycopg2 import sql

sql_update_query = sql.SQL(
    """update "YP_SUPPLIERS" 
          set {} = %s,
              {} = %s
        where "YP_SUPPLIERS"."supplierID" = %s""").format(
                                                    sql.Identifier(key[0]),
                                                    sql.Identifier(key[1])
                                                   )

cursor.execute(sql_update_query, (value[0], value[1], supplierID))                        

Use this

sql_update_query = """update  YP_SUPPLIERS set %s =%s where supplierID= %s"""
cursor.execute(sql_update_query%(key[0],value[0],supplierID))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM