Python Lambda函数成功,但未更新任何行

2024-05-23 20:21:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个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)

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


Tags: 函数awseventidputdefpostgresconnection
1条回答
网友
1楼 · 发布于 2024-05-23 20:21:59

您需要调用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()

相关问题 更多 >