How to get number of rows affected, while executing MySQL query from bash?

MysqlBash

Mysql Problem Overview


I know how one can execute MySQL queries / commands from bash:

mysql -u[user] -p[pass] -e "[mysql commands]"

or

mysql -u[user] -p[pass] `<<`QUERY_INPUT

[mysql commands]

QUERY_INPUT

How can I capture how many rows were affected by the query?
I tried doing:

variable='`mysql -u[user] -p[pass] -e "[mysql commands]"`'

It does execute the command but it does not return the number of affected rows.

Mysql Solutions


Solution 1 - Mysql

Put

SELECT ROW_COUNT();

as the last statement in your batch and parse the output

Solution 2 - Mysql

I might have answered myself the question, been looking at the parameters, and aparently using "-v -v -v" as parameters to the mysql command forces it to be more verbose and it spits out how many rows where affected.

Solution 3 - Mysql

Not an answer, but useful addition, you also could try the other MySQL information functions ( which include ROW_COUNT() ) to give you specific information you require. See MySQL reference here

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionflorin.bunauView Question on Stackoverflow
Solution 1 - MysqlKen KeenanView Answer on Stackoverflow
Solution 2 - Mysqlflorin.bunauView Answer on Stackoverflow
Solution 3 - MysqlJasonView Answer on Stackoverflow