簡體   English   中英

如何在 python “”” 引號字符串中添加轉義?

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

我正在嘗試集成我的 AWS lambda function 來查詢我的 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))

顯然在創建使用的表“”時。 解決這個問題非常忙碌。

錯誤

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

似乎設置 %s 的值是供應商名稱應該在“”中。 任何人都可以讓我知道解決方法或我該如何正確實施

使用 psycopg2 的SQL 組合功能

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))                        

用這個

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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