LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
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 11-07-2008, 07:05 PM   #1
word_virus
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Rep: Reputation: 0
Iterate over SQL results in bash script. Is there a better way to do this?


Hey, all.

So, I've got a shell script that I'm using to update a table in a MySQL database. It runs a select, reads in the results one line at a time, and uses them as params in an update statement. I'm using AWK to grab the fields I need from the output of the select and put them in the proper order in the update statement. Which works -- but it feels like a hack. Is there a better way to do this?
Example follows:

Code:
/usr/bin/mysql --skip-column-names -u$SQL_USR -p$SQL_PASS -D$SQL_DB -e"SELECT id,name FROM $SQL_TABLE" |
while read FN; do
echo "UPDATE users SET foo = bar WHERE id = `echo $FN | awk '{ print $1 }'` and name = `echo $FN | awk '( print $2 }'`"
done
Thanks in advance for your ideas.
 
Old 11-07-2008, 10:56 PM   #2
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
you can do a subquery insert...

INSERT INTO users (id, name) (SELECT id,name FROM $SQL_TABLE)
 
Old 11-07-2008, 11:04 PM   #3
graemef
Senior Member
 
Registered: Nov 2005
Location: Hanoi
Distribution: Fedora 13, Ubuntu 10.04
Posts: 2,379

Rep: Reputation: 148Reputation: 148
IN MySQL you can use IGNORE to ignore duplicates or ON DUPLICATE UPDATE ... to update the value, thus you would have something like:
Code:
INSERT INTO users (id, name) 
   SELECT $SQL_TABLE.id,$SQL_TABLE.name
   FROM $SQL_TABLE 
   ON DUPLICATE KEY UPDATE user.id=$SQL_TABLE.id, user.name = $SQL_TABLE.name
 
Old 11-09-2008, 09:52 AM   #4
jan61
Member
 
Registered: Jun 2008
Posts: 235

Rep: Reputation: 47
Moin,

Quote:
Originally Posted by graemef View Post
...
Code:
INSERT INTO users (id, name) 
   SELECT $SQL_TABLE.id,$SQL_TABLE.name
   FROM $SQL_TABLE 
   ON DUPLICATE KEY UPDATE user.id=$SQL_TABLE.id, user.name = $SQL_TABLE.name
But this will perform an insert on non existing rows - the original post did not do it.

@graemef: At least you should avoid calling awk twice for each line of your input. You can do the whole work in one awk:
Code:
/usr/bin/mysql ... | awk ' \
  { printf "UPDATE users SET foo = bar WHERE id = %d and name = \'%s\'\n",
           $1, $2; } '
Jan
 
Old 11-09-2008, 10:15 AM   #5
word_virus
LQ Newbie
 
Registered: Nov 2008
Posts: 2

Original Poster
Rep: Reputation: 0
Quote:
Originally Posted by jan61 View Post
Code:
/usr/bin/mysql ... | awk ' \
  { printf "UPDATE users SET foo = bar WHERE id = %d and name = \'%s\'\n",
           $1, $2; } '
That does look a little less ugly. Thank you.
 
  


Reply



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
Another Bash iterate lines to array... jchambers Programming 9 08-22-2007 02:06 PM
killing child processes of a bash script results in strange random kills omnio Programming 6 03-12-2007 07:35 AM
Using PHP, run bash script and see results on Server's Monitor xmrkite Linux - Software 6 10-17-2006 01:28 PM
Iterate through regular expression results in Perl rose_bud4201 Programming 1 04-14-2006 01:47 PM
Bash: Iterate through $@ (easy++) beatnik007 Programming 2 04-03-2004 01:39 AM

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

All times are GMT -5. The time now is 07:08 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
Open Source Consulting | Domain Registration