0

new to backend coding. I am running a python lambda function which POSTs rows to my database. I want to send a response to my JavaScript frontend. Either a 200 if the POST was successful or a 400 if it was not. I'm not sure how my function knows if the post was successful or not.

Currently I have this:

    postgres_insert_query = "BEGIN; INSERT INTO clients (id, name, phone, contact) VALUES ('{0}','{1}','{2}','{3}') ON CONFLICT (id) DO UPDATE SET name='{1}', phone='{2}', contact='{3}';".format(data['id'], data['name'], data['phone'], data['contact']);
    postgres_insert_query2 = "INSERT INTO address (line1, city, state, zip, uuid) VALUES ('{0}','{1}','{2}', {3}, '{4}') ON CONFLICT (uuid) DO UPDATE SET line1='{0}', city='{1}', state='{2}', zip='{3}';".format(address['line1'], address['city'], address['state'], address['zip'], data['id']);
    postgres_insert_query3 = "INSERT INTO images (logo, background_image, uuid) VALUES ('{0}','{1}','{2}') ON CONFLICT (uuid) DO UPDATE SET logo='{0}', background_image='{1}';".format(data['logo'], data['background_image'], data['id']);
    postgres_insert_query4 = "INSERT INTO hours (monday_start, monday_end, tuesday_start, tuesday_end, wednesday_start, wednesday_end, thursday_start, thursday_end, friday_start, friday_end, saturday_start, saturday_end, sunday_start, sunday_end, uuid) VALUES ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}','{11}','{12}','{13}','{14}') ON CONFLICT (uuid) DO UPDATE SET monday_start='{0}', monday_end='{1}', tuesday_start='{2}', tuesday_end='{3}', wednesday_start='{4}', wednesday_end='{5}', thursday_start='{6}', thursday_end='{7}', friday_start='{8}', friday_end='{9}', saturday_start='{10}', saturday_end='{11}', sunday_start='{12}', sunday_end='{13}'; COMMIT;".format(hours['mondayStart'], hours['mondayEnd'],hours['tuesdayStart'], hours['tuesdayEnd'], hours['wednesdayStart'], hours['wednesdayEnd'], hours['thursdayStart'], hours['thursdayEnd'], hours['fridayStart'], hours['fridayEnd'], hours['saturdayStart'], hours['saturdayEnd'], hours['sundayStart'], hours['sundayEnd'], data['id']);
    query = postgres_insert_query+postgres_insert_query2+postgres_insert_query3+postgres_insert_query4;
    print(event)
    cursor.execute(query)
    print(cursor.rowcount)
    connection.commit()                          
    return {
        'statusCode': 200,
         'headers': {
            "Access-Control-Allow-Origin" : "*"
         },
    }

I think I need to update the return block but I am not sure how to view the status of the query.

tdammon
  • 610
  • 2
  • 13
  • 39
  • If the `commit()` succeeds then the insertion was successful (if it doesn't succeed it will raise an exception). I'd suggest reviewing [this Q&A](https://stackoverflow.com/q/902408/5320906) on safely injecting values into SQL statements, or these sections from the psycopg2 docs, if that's what you are using to connect to the database: [1](https://www.psycopg.org/docs/usage.html#passing-parameters-to-sql-queries) [2](https://www.psycopg.org/docs/usage.html#the-problem-with-the-query-parameters). – snakecharmerb Sep 23 '22 at 06:43

0 Answers0