I am trying to execute multiple query in a for-loop. So in each loop, if either query failed to execute, all queries in that loop will not commit to database and return the error message for failed query. Below is my code. Seems even there is one query failed to run, the others queries will still execute and commit the data to database. How can I change it to achieve what I want? Thanks
Code:
import mysql.connector
mydb = mysql.connector.connect()
cursor = mydb.cursor()
for i in country_list:
try:
query1 = "call_sp_insert_sth_1" //insert data to DB
query2 = "call_sp_insert_sth_2" //insert data to DB
query3 = "call_sp_insert_sth_3" //update data to DB
cursor.execute(query1)
cursor.execute(query2)
cursor.execute(query3)
mydb.commit()
except Exceptiion as err:
fail_list.append('error':err.msg)
continue
mysql.connector.close_connection(mydb)