LinuxQuestions.org
Share your knowledge at the LQ Wiki.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 05-26-2011, 03:26 PM   #1
z01krh
Member
 
Registered: May 2009
Posts: 34

Rep: Reputation: 0
bash to loop thorouh mysql select array


I need to know how to assign a result from a select. I am clueless on the sytax. I am trying this in bash can you please help.
Maybe I am not assigning the array right. It gives me the whole row in the echo instead of just field a. How do I get fieldA = a in the select. Note script was stripped for security on database info but the syntax is same.

Code:
#!/bin/sh

results="$(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse 'select a,b,c,d from tblMytable')"

for rows in "${results[@]}"
do
  fieldA=${rows[0]};
  echo ${fieldA};
done
 
Old 05-26-2011, 03:58 PM   #2
z01krh
Member
 
Registered: May 2009
Posts: 34

Original Poster
Rep: Reputation: 0
Ok I found one way to do this

Code:
#!/bin/sh

results="$(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse 'select a,b,c,d from tblMytable')"

for rows in "${results[@]}"
do
  fieldA=`echo ${rows}| awk '{print $1}'`;
  fieldB=`echo ${rows}| awk '{print $2}'`;

  echo ${fieldA} ${fieldB};
done
]

Doesn't bash have some kind of true array there has to be a better way.
 
Old 05-26-2011, 04:33 PM   #3
z01krh
Member
 
Registered: May 2009
Posts: 34

Original Poster
Rep: Reputation: 0
solution Thought I would post my own solution to save someone else hours of searching.

Code:
#!/bin/sh

results=($(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse "select a,b,c,d from myTable;"))

cnt=${#results[@]}

for (( i=0 ; i<${cnt} ; i++ ))
do
    echo "Record No. $i: ${results[$i]}"

    fieldA=${results[0]};
    fieldB=${results[1]};
    fieldC=${results[2]};
    fieldD=${results[3]};

done

echo $fieldA $fieldB $fieldC $fieldD
Output:

Record No. 0: a
Record No. 1: b
Record No. 2: c
Record No. 3: d
 
Old 12-27-2017, 08:30 PM   #4
IncIZXhgCp24
LQ Newbie
 
Registered: Dec 2017
Posts: 1

Rep: Reputation: Disabled
The code posted in #3 does interpret spaces within records correctly. Given a record with spaces, it will be split into different records by the position of the spaces.
Code:
#!/bin/sh

results=($(mysql --user ${DB_USER} -p${DB_PWD} ${DB_NAME} -Bse "select a,b,c,d from myTable;"))

cnt=${#results[@]}

for (( i=0 ; i<${cnt} ; i++ ))
do
    echo "Record No. $i: ${results[$i]}"

    fieldA=${results[0]};
    fieldB=${results[1]};
    fieldC=${results[2]};
    fieldD=${results[3]};

done

echo $fieldA $fieldB $fieldC $fieldD
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
simple BASH while loop question / Array riotxix Programming 10 01-03-2010 08:57 PM
bash - loop over variable array names talanis Programming 2 02-19-2009 11:09 AM
bash loop within a loop for mysql ops br8kwall Programming 10 04-30-2008 03:50 AM
BASH Need help figuring out how to loop SELECT statement mcdrr Programming 2 05-08-2007 04:47 AM
Bash: how to test for a number in Array within if loop? realos Programming 1 12-15-2006 07:59 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:39 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration