+ 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šŸŽ­
JacešŸŽ­ - avatar
4 Answers
+ 2
This should help: How to use START TRANSACTION, COMMIT, and ROLLBACK in MySQL https://dev.mysql.com/doc/refman/5.7/en/commit.html
24th Oct 2021, 10:21 PM
Brian
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
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
0
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šŸŽ­
JacešŸŽ­ - avatar