0

I have an AWS lamda function making an update to my 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)

The print statement prints 1 as expected however when I look at the data in my database no updates were made.

0

1 Answer 1

1

you need to call connection.commit() to reflect the change in database

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()
Sign up to request clarification or add additional context in comments.

1 Comment

I get the following error: "errorMessage": "'psycopg2.extensions.cursor' object has no attribute 'commit'"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.