I have a mysql table with some unique constraints. I have a script that asks some questions, and then inserts the answers into the mysql table.
I don't know how to tell the script to do something else if a duplicate entry exists.
I'd like to give the option to remove the entry and add the new, or return to the start begin the script again.
Hope that makes sense.
Preston
I would suggest running a query to see if the record exists first and then you can have some bash logic to decide what to do based on the result of the query. Maybe something like this...
Using
--batch
and-N
will remove the formatting so that we can assign the result to a bash variable and then decide what to do from there.The other option is to check the exit status code of the mysql insert and, if it fails, decide what to do from there.
Further reading: get the sql query result with out table format | Stack Overflow