LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - General
User Name
Password
Linux - General This Linux forum is for general Linux questions and discussion.
If it is Linux Related and doesn't seem to fit in any other forum then this is the place.

Notices


Reply
  Search this Thread
Old 12-14-2012, 02:41 AM   #1
pranjal_815
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Rep: Reputation: Disabled
Unhappy MySQL Query result question


Hi Guys,

I was working on shell script in which I have multiple Insert is performing on single table getting data from different table. But sometime it works correctly and sometime it does not.

I am in search is there any way through which I can story my query result into a file. Please do not take me wrong by the words query result, as here Query Result means the result that you get when you run your query i.e, 12 rows effected.

I have tried bla bla method but it did not work for me.

For your help i m providing you the sample :

Query is as follows :

INSERT INTO table_name1(fk_req_id,cli,points,source,gift_redeemed,gift_code,points_befor_redmptn,points_after_re dmptn,NAME,address,city,state,pin,age,dateofbirth,brand,COMMENT,intensity,aniversary,education,servi ce,business,completestatus,update_date)select fk_req_id,cli,ttl_points,'PANEL',gift_redeemed,gift_code,points_befor_redmptn,points_after_redmptn,n ame,address,city,state,pin,age,dateofbirth,curr_barand,comment,intensity,aniversary,education,servic e,business,completestatus,insert_date from table_name2 where date(insert_date) = date_sub(current_date,interval 4 day)" >>$myfile

Here $myfile is the log path where I want to write the logs or redirect my output.


Any help and suggestion would be helpful.

regards,
Pranjal

Last edited by onebuck; 12-14-2012 at 04:31 PM. Reason: correct title from move post actions by mod
 
Old 12-15-2012, 09:03 AM   #2
Habitual
LQ Veteran
 
Registered: Jan 2011
Location: Abingdon, VA
Distribution: Catalina
Posts: 9,374
Blog Entries: 37

Rep: Reputation: Disabled
Quote:
I was working on shell script in which I have multiple Insert is performing on single table getting data from different table. But sometime it works correctly and sometime it does not.
Redirecting "12 rows effected" using a piped redirect is NOT the way to do debugging of shell scripts.

Quote:
I am in search is there any way through which I can story my query result into a file
What query is that? You are doing inserts.
It's already in a file. (see "working on shell script")

Quote:
I have tried bla bla method but it did not work for me.
Never heard of it. Did you author this method too?

Redirecting a shell script for an "Insert into" statement(s), near as I can remember,
the only output IS "nn rows effected"
 
Old 12-15-2012, 11:47 PM   #3
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
I'm curious as well what method 'bla bla' as there is a risk that the below link does method 'bla bla'.

As you can pipe the output of the bash mysql command to grep (as shown in http://www.cyberciti.biz/faq/using-m...shell-scripts/), I'm quite sure you can redirect it to a file.

No mysql at hand to test, though.

PS
It can be useful to post the relevant part of your code.

Last edited by Wim Sturkenboom; 12-15-2012 at 11:48 PM. Reason: Added PS
 
Old 12-16-2012, 11:51 PM   #4
pranjal_815
LQ Newbie
 
Registered: Jun 2012
Posts: 4

Original Poster
Rep: Reputation: Disabled
@Habitual
Quote:
I am in search is there any way through which I can story my query result into a file
Quote:
What query is that? You are doing inserts.
It's already in a file. (see "working on shell script")
I am using Insert Query. I want the o/p of the Insert to be saved in a file so that i can keep track of the information.

Quote:
I have tried bla bla method but it did not work for me.
Here bla bla means different methods that I found on net but none of them work for me.


If you have command to do so then please share the procedure through which I can do it.

Regards,
Pranjal
 
Old 12-18-2012, 10:04 AM   #5
Wim Sturkenboom
Senior Member
 
Registered: Jan 2005
Location: Roodepoort, South Africa
Distribution: Ubuntu 12.04, Antix19.3
Posts: 3,794

Rep: Reputation: 282Reputation: 282Reputation: 282
An insert query on the command line does not return any message about the number of inserted rows. The only thing you can do is to check if the query was executed succesfully.
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null);"
Enter password: 
wim@VirtualBox:~$ echo $?
0
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null);"
Enter password: 
ERROR 1045 (28000): Access denied for user 'root'@'localhost' (using password: YES)
wim@VirtualBox:~$ echo $?
1
wim@VirtualBox:~$
What you're after is probably the following.
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null); select row_count()"
Enter password: 
+-------------+
| row_count() |
+-------------+
|           1 |
+-------------+
Depending on your needs, you can make it a bit easier for further parsing
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13',null); select row_count()\G"
Enter password: 
*************************** 1. row ***************************
row_count(): 1
wim@VirtualBox:~$
You can get rid of the '**....**' using grep -v and you can get rid of the row_count() using cut (both used in the example below)
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13','1989-05-13'); select row_count()\G" | cut -d: -f 2 |grep -v "\*"
Enter password: 
 1
wim@VirtualBox:~$
And lastly you can redirect the output to a file
Code:
wim@VirtualBox:~$ mysql -u root -p menagerie -e "insert into pet values('Buffy','Harold','dog','f','1989-05-13','abc'); select row_count()\G" | cut -d: -f 2 |grep -v "\*" > mysql.test.txt
Enter password: 
wim@VirtualBox:~$ cat mysql.test.txt 
 1
wim@VirtualBox:~$
Hope this helps

PS
Please pay attention to the notes on http://dev.mysql.com/doc/refman/5.0/...tion_row-count

PPS
Obviously you need to change the user and the database

Last edited by Wim Sturkenboom; 12-18-2012 at 12:08 PM. Reason: Added PPS and fixed mistake in query
 
  


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 On
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
[SOLVED] MySQL make a row first in result of query Skyer Programming 2 08-12-2011 06:31 AM
[SOLVED] Change a mysql query result prior to display hattori.hanzo Programming 4 10-19-2010 05:46 AM
How do I output the result of a query in php? orfiyus Programming 0 07-19-2007 02:33 PM
select query thru php outputs no result suchi_sood Programming 1 04-11-2006 10:21 AM
mySQL redirecting query result to a .txt file! buttersoft Linux - Software 8 12-12-2003 03:05 AM

LinuxQuestions.org > Forums > Linux Forums > Linux - General

All times are GMT -5. The time now is 06:01 AM.

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