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


Question

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.

1
52
2/1/2017 1:01:02 AM

Accepted Answer

Put

SELECT ROW_COUNT();

as the last statement in your batch and parse the output

84
7/5/2009 11:26:35 AM

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.


Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon