views:

733

answers:

4

I need a variable to hold results retrieved from the database. So far this is basically what I'm trying with no success.

myvariable=$(mysql database -u $user -p $password | SELECT A, B, C FROM table_a)

My understanding of bash commands is not very good as you can see.

+1  A: 

You have the pipe the other way around and you need to echo the query, like this:

myvariable=$(echo "SELECT A, B, C FROM table_a" | mysql db -u $user -p $password)

Another alternative is to use only the mysql client, like this

myvariable=$(mysql db -u $user -p $password -se "SELECT A, B, C FROM table_a")

(-s is required to avoid the ASCII-art)

Now, BASH isn't the most appropriate language to handle this type of scenarios, especially handling strings and splitting SQL results and the like. You have to work a lot to get things that would be very, very simple in Perl, Python or PHP.

For example, how will you get each of A, B and C on their own variable? It's certainly doable, but if you do not understand pipes and echo (very basic shell stuff), it will not be an easy task for you to do, so if at all possible I'd use a better suited language.

Vinko Vrsalovic
A: 

I don't know much about the mysql command line interface, but assuming you only need help with the bashing, you should try to either swap the commands around like so:

myvariable=$(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)

which echos the string into mysql. Or, you can be more fancy and use some new bash-features (the here string)

myvariable=$(mysql database -u $user -p $password<<<"SELECT A, B, C FROM table_a")

resulting in the same thing (assuming you're using a recent enough bash version), without involving echo.

Note that myvariable will contain everything that mysql outputs on standard out (usually everything but error messages), including any and all column headers, ascii-art frames and so on, which may or may not be what you want.

EDIT:
As has been noted, there appears to be a -e parameter to mysql, I'd go for that one, definately.

roe
Thanks, the mysql command also did not like the -p.. I needed to use --password=$password.
imnotneo
+1  A: 

A more direct way would be:

myvar = $(mysql mydatabase -u $user -p $password -se "select a, b, c from table_a")
ennuikiller
good call, +1 from me
roe
This will print all ASCII art, you need to supply -s as well (it's natural not to want the ASCII art if you'll be doing some processing with the data)
Vinko Vrsalovic
Vinko is right, corrected the answer
ennuikiller
Oh, and you are missing the database parameter to the mysql client :)
Vinko Vrsalovic
thanks vinko, added that as well!!
ennuikiller
A: 

To read the data line-by-line into a Bash array you can do this:

while read -a row
do
    echo "..${row[0]}..${row[1]}..${row[2]}.."
done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)

Or into individual variables:

while read a b c
do
    echo ""..${a}..${b}..${c}.."
done < <(echo "SELECT A, B, C FROM table_a" | mysql database -u $user -p $password)
Dennis Williamson