LinuxQuestions.org
Welcome to the most active Linux Forum on the web.
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices


Reply
  Search this Thread
Old 08-01-2010, 02:11 PM   #1
chirayu11
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Rep: Reputation: 0
Running SQL commands from within bash


Hi

I would be running SQL commands (UPDATE/SELECT) from within my bash script. I am completely new to this subject.

Is MYSQL used for this purpose? Alternatively, what is sqlplus?

Any help appreciated. Thanks.
 
Old 08-01-2010, 02:19 PM   #2
paulsm4
LQ Guru
 
Registered: Mar 2004
Distribution: SusE 8.2
Posts: 5,863
Blog Entries: 1

Rep: Reputation: Disabled
Hi -

You need some kind of "client" in order to talk to your database. This could be a program you write, or a program that comes with the database.

"mysql" is a text-mode client that can talk to a Mysql database. "sqlplus" is a text-mode client that can talk to an Oracle database.

Both mysql and sqlplus can be used from shell scripts - it's just a question of what kind of database you have (i.e. whether you need to communicate with Oracle, with MySql, or with something else).

'Hope that helps .. PSM
 
Old 08-02-2010, 03:05 AM   #3
AnanthaP
Member
 
Registered: Jul 2004
Location: Chennai, India
Distribution: UBUNTU 5.10 since Jul-18,2006 on Intel 820 DC
Posts: 855

Rep: Reputation: 203Reputation: 203Reputation: 203
mysql is the name for a particular brand of rdbms. sqlplus is the client program for oracle.

In general xsql is the name of the client program (like sqlplus), then you can program like this in your bash script.


Code:
.
.
.
# now invoking sqlplus
sqlplus  datbasename < XX
sqlcommand1
sqlcommand2
.
.
XX
# come out of sqlplus
.
.
Note the XX. It can be any set of characters. But if you start with XX, you must end with XX and bash requires that the ending XX be in column no 1.
 
Old 08-02-2010, 03:23 AM   #4
EricTRA
LQ Guru
 
Registered: May 2009
Location: Gibraltar, Gibraltar
Distribution: Fedora 20 with Awesome WM
Posts: 6,805
Blog Entries: 1

Rep: Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295Reputation: 1295
Hello,

Another example, like I use it in Bash scripts to talk to our database servers:

For Oracle using SQLPLUS:
Code:
sqlplus -s user/password@SID <<EOF
YOUR SQL QUERY;
EOF
For MySQL:
Code:
mysql -udbuser -ppassword -hhostname -v -e 'show slave status\G'
Kind regards,

Eric
 
Old 08-02-2010, 06:42 AM   #5
chrism01
LQ Guru
 
Registered: Aug 2004
Location: Sydney
Distribution: Centos 6.9, Centos 7.3
Posts: 17,417

Rep: Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397Reputation: 2397
Another method is to just invoke an sql file eg
Code:
mysql -udbuser -ppassword -hhostname <some_file.sql

# where the sql file content might be
select * from some_table;
You can use the same basic idea for most (all?) relational DBs.
 
Old 08-02-2010, 07:31 AM   #6
chirayu11
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Hi,

Thanks for all you replies. It really helped.

One further question: I am about to run a handful of SQL commands (update and select) from within my bash script. The database I am using is mysql. I can execute the commands like these:
mysql -uMyUserName -pPassword -DDatabaseName -e "SQLQuery"

But I have several of these queries and I login several times. How do I login just once and execute queries and then log out?

Thanks in advance.
 
Old 08-02-2010, 07:38 AM   #7
chirayu11
LQ Newbie
 
Registered: Jul 2010
Posts: 8

Original Poster
Rep: Reputation: 0
Found the answer to my own question. The way to do it is:

Code:
mysql -u[user] -p[pass] -D[dbName] << QUERY_INPUT

#sql commands go here
select id, name from employee;
select crap from table;    

QUERY_INPUT
 
  


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
Running bash but common bash commands not working michellepace Linux - Newbie 11 07-17-2010 10:19 AM
[SOLVED] My simple bash script is not running all of the commands Subject16 Programming 5 06-14-2010 11:17 AM
Running commands from array in a child bash script bengoavs Programming 2 10-26-2007 03:16 PM
Running bash commands from php. rookiepaul Linux - General 3 03-22-2006 03:22 AM
Running Bash Commands From Apache windisch Linux - Software 14 08-17-2005 04:02 PM

LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie

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