Mysql – Check if remote thesql query was successful – bash

bashMySQL

When using a bash script to execute remote MySQL queries, how can I know if the command was successful or not. Locally it would return an exit code. However, remotely it seems to send the query and if it is able to connect to the remote MySQL database it takes it as successful. Is there any way to view the output when executing remotely, like when executing locally. Here is the script:

#!/bin/bash

RemoveID=`mysql -u root -proot -h 192.168.1.56 -e "delete from table where ID = '$1'"`

Best Answer

SELECT statements will return number of rows into your shell variable.

For DELETE statements just append a SELECT ROW_COUNT() after your mysql query, so it would be in your example like:

RemoveID=`mysql -u root -proot -h 192.168.1.56 -e "delete from table where ID = '$1';select row_count()"`

echo $RemoveID ROW_COUNT() 1