LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
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 03-16-2019, 06:27 AM   #1
Julie1988
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Rep: Reputation: Disabled
shell scripting: how to receive multiple records from a sql query


When I run a query (return values) rv=$($mysql -u$user -p$pwd stocks<<<$query), has multiple records in database for the same query but the response for this code is a single line. Would like to see response exactly the same way as in the db. Appreciate help.
 
Old 03-16-2019, 06:44 AM   #2
MadeInGermany
Senior Member
 
Registered: Dec 2011
Location: Simplicity
Posts: 1,029

Rep: Reputation: 453Reputation: 453Reputation: 453Reputation: 453Reputation: 453
How did you display the result?
Correct is
Code:
echo "$rv"
The quotes prevent the shell from word splitting/joining and doing substituitions.
For the same reason consider further quotes:
Code:
rv=$("$mysql" -u"$user" -p"$pwd" stocks<<<"$query")
 
1 members found this post helpful.
Old 03-16-2019, 06:51 AM   #3
ondoho
LQ Addict
 
Registered: Dec 2013
Posts: 11,054
Blog Entries: 7

Rep: Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791Reputation: 2791
...and lastly:
Code:
rv="$("$mysql" -u"$user" -p"$pwd" stocks<<<"$query")"
echo "$rv"
 
1 members found this post helpful.
Old 03-16-2019, 07:40 AM   #4
Julie1988
LQ Newbie
 
Registered: Mar 2019
Posts: 3

Original Poster
Rep: Reputation: Disabled
I get a single array

Count of rv is still 1 count="${#rv[@]}". The responses helped to a point but when I loop on rv, I still get everything in 1 record,

type date qty(lb)
meat 2019-01-18 1176
beaf 2019-01-18 2013
chicken 2019-01-18 5144

need to get 1 record at a time for further processing.
 
Old 03-16-2019, 08:49 AM   #5
michaelk
Moderator
 
Registered: Aug 2002
Posts: 18,217

Rep: Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545Reputation: 2545
$rv is a string, not an array or a record. You can create an array with each line being an element of the array with the command
Code:
IFS=$'\n' read -rd '' -a y <<<"$rv"
From there you still need to split the string by column. You can also iterate $rv in a loop
Code:
while IFS= read -r line
do 
  echo $line
done <<< "$rv"
 
1 members found this post helpful.
  


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
[SOLVED] MX Records / A Records / CNAME Records - Advice Please fusion1275 Linux - Newbie 15 01-18-2011 04:06 AM
Reading caller detail records(CDR ) records eliufoo Linux - Networking 2 12-16-2008 11:31 PM
DNS Zone Records - MX Records ddzc Linux - Software 8 05-24-2007 01:26 PM
1+0 records in how to disiable in DD command "1+0 records out" Kotresh Linux - Software 3 03-08-2007 08:57 AM
How to compare records in two tables in seperate My Sql database using shell script sumitarun Programming 5 04-14-2005 09:45 AM

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

All times are GMT -5. The time now is 10:53 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