English 中文(简体)
Bash 方言
原标题:Bash Script Loop Through MySQL

I need a bash script that can retrieve MySQL data from a remote data base. Actually I have that done, but what I need it to do now is loop through the records somehow and pass a variable to another bash file. Here s my MySQL call:

mysql -X -u $MyUSER -p$MyPASS -h$MyHOST -D$MyDB -e SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`="1" AND `theme_compiled`= 0 ;  > themes.xml
download_themes.sh

现在,我正试图用某种方式通过数据整理数据。 我试图避免PHP和 per,只是试图使用ash。 提前感谢。

最佳回答

例如:

mysql -e "SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`= 1  AND `theme_compiled`= 0 " | while read theme_name guid; do
    # use $theme_name and $guid variables
    echo "theme: $theme_name, guid: $guid"
done

in short: the mysql command outputs record separated by and fields separated by when the output is a pipe. the read command reads a line, splits in fields, and puts each on a variable.

如果您的数据在各领域有空间,那么你会遇到一些缺省代码(<>>>>>>> /代码(>分立)的问题。 这里有一些办法;但是,如果你只读两个领域,其中一只应该拥有任何空间(如<条码>><<>id>/code>),那么你就可以将危险领域放在末尾,而<条码><>>><>>>则将超出最后几个变量。

类似:

mysql -e "SELECT `guid` `theme_name`, FROM `themes` WHERE `theme_purchased`= 1  AND `theme_compiled`= 0 " | while read guid theme_name; do
    # use $theme_name and $guid variables
    echo "theme: $theme_name, guid: $guid"
done
问题回答

Rather than outputting XML, may I suggest you simply use the SELECT INTO OUTFILE syntax or mysql --batch --raw to output tab-separated values. You then have much easier access through bash to the rest of the Unix toolchain, like cut and awk to retrieve the fields you need and reuse them with Bash. No other scripting language is necessary and you needn t mess with XML.

mysql --batch --raw -u $MyUSER -p$MyPASS -h$MyHOST -D$MyDB -e SELECT `theme_name`, `guid` FROM `themes` WHERE `theme_purchased`="1" AND `theme_compiled`= 0 ;  
| awk  {print "theme: " $1  " guid: " $2} 

接受的答案在空间投入产出时不可行。 这是一种简单的固定办法(IFS=$)。 纽约总部 附注——是:


>mysql ... -BNr -e "SELECT  funny man ,  wonderful  UNION SELECT  no_space ,  I love spaces ;" | while IFS=$ 	  read theme_name guid; do echo "theme: $theme_name guid: $guid"; done
theme: funny man guid: wonderful
theme: no_space guid: I love spaces

当然,你要替代你自己的问询。

管道——尽管是危险的,但循环内的变化在另一个分处理中发生,不会在目前的文字中生效。

顺便说一句,我对转向外部档案解决办法表示仇恨。

我建议使用“

while read field1 field2 field3
do
done < <(mysql -NB -e "$sql")
#     ^
#   space needed




相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

php return a specific row from query

Is it possible in php to return a specific row of data from a mysql query? None of the fetch statements that I ve found return a 2 dimensional array to access specific rows. I want to be able to ...

Character Encodings in PHP and MySQL

Our website was developed with a meta tag set to... <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" /> This works fine for M-dashes and special quotes, etc. However, I ...

Pagination Strategies for Complex (slow) Datasets

What are some of the strategies being used for pagination of data sets that involve complex queries? count(*) takes ~1.5 sec so we don t want to hit the DB for every page view. Currently there are ~...

Averaging a total in mySQL

My table looks like person_id | car_id | miles ------------------------------ 1 | 1 | 100 1 | 2 | 200 2 | 3 | 1000 2 | 4 | 500 I need to ...

热门标签