MySQL hard question | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

MySQL hard question

It's there a way to rollback automatically if a query fails?? The values in my query can change What I'm trying to say is that my query is being modified by placeholder %s I want to rollback if any of the queries fails... I've been looking for a while and haven't found a solution... 😢

24th Oct 2021, 9:01 PM
Jace🎭 - avatar
4 Answers
+ 2
This should help: How to use START TRANSACTION, COMMIT, and ROLLBACK in MySQL
24th Oct 2021, 10:21 PM
Brian - avatar
+ 1
Try using: SET autocommit=0; Then your query, and if it fails, then use: ROLLBACK; But I think you should test it first by inserting a "test row" and then delete it again.
24th Oct 2021, 9:47 PM
Jan - avatar
+ 1
I’m not an expert about mysql but what about bulk operations? I understand that you are trying to do some inserts. Use a single transaction to englobe all these operations. If any of the operations fails then the transaction fails
25th Oct 2021, 8:53 AM
Guillem Padilla
Guillem Padilla - avatar
Update I just found that there are no autorollbacks in mysql so I have to code that behavior now I'm looking for a solution that's checks if one query works to run another and if the second fails revert the changes
24th Oct 2021, 9:32 PM
Jace🎭 - avatar