mysql in bash script
Hi,
when I want to query some mysql database from bash, I can use the following construction as long as the query is simple: QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select * from table where userid=10')" How do I manage to do something similar when the query itself allready has quotes? Like: select * from table where created='2011-10-10 09:00:05' . Now QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select * from table where created=`2011-10-10 09:00:05` and userid=10')" doesn't work. Regards, slibbe |
Instead of having 2011-10-10 09:00:05 in quotes, you should store it in a variable instead. Also, you used backticks in your example, instead of single quotes; Anything within backticks will be executed.
|
Code:
SEL="select username, articleid" select username, articleid from table where userid=10 and created='2011-10-10 09:00:05' However, a line like Code:
mysql -u$USER -p$PASS $DB -e "$QRY" Code:
mysql -u$USER -p$PASS $DB -e $QRY |
I got it!
Instead of Code:
QRY="$(mysql -u$USER -p$PASS $DB -Bse 'select col1,col2,col3,datetime from table where col1=5 and datetime like '2010-04-01%' ')" Code:
QRY=$(mysql -u$USER -p$PASS $db -e "select col1,col2,col3,datetime from table where col1=5 and datetime like '2010-04-01%'") '\''2010-04-01%'\'' instead of '2010-04-01%' (all single quotes). Couldn't stand it not to find an answer for such a common problem :) |
All times are GMT -5. The time now is 03:00 PM. |