【问题标题】:Python Lambda function succeeds but no rows appear updatedPython Lambda 函数成功,但未显示任何行已更新
【发布时间】:2021-07-14 05:31:50
【问题描述】:

我有一个 AWS lamda 函数正在更新我的 RDS。

def lambda_handler(event, context):

    connection = psycopg2.connect(user="****", password=****,
                                  host=****, port="****",
                                  database="****")
    
    cursor = connection.cursor()
    postgres_put_query = "UPDATE restaurant SET (item, price) = ('{0}','{1}') WHERE id = '{2}'".format(event['item'], event['price'], event['id'])
    cursor.execute(postgres_put_query)
    print(cursor.rowcount)

打印语句按预期打印1,但是当我查看数据库中的数据时,没有进行任何更新。

【问题讨论】:

    标签: python aws-lambda amazon-rds psycopg2


    【解决方案1】:

    你需要调用connection.commit()来反映数据库的变化

    def lambda_handler(event, context):
    
        connection = psycopg2.connect(user="****", password=****,
                                      host=****, port="****",
                                      database="****")
        
        cursor = connection.cursor()
        postgres_put_query = "UPDATE restaurant SET (item, price) = ('{0}','{1}') WHERE id = '{2}'".format(event['item'], event['price'], event['id'])
        cursor.execute(postgres_put_query)
        print(cursor.rowcount)
        connection.commit()
    

    【讨论】:

    • 我收到以下错误:“errorMessage”:“'psycopg2.extensions.cursor' 对象没有属性 'commit'”
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-02-08
    • 2022-09-27
    • 2012-10-02
    • 2018-09-09
    • 1970-01-01
    • 2012-12-14
    相关资源
    最近更新 更多