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. |
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 |
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:
. |
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 Code:
mysql -udbuser -ppassword -hhostname -v -e 'show slave status\G' Eric |
Another method is to just invoke an sql file eg
Code:
mysql -udbuser -ppassword -hhostname <some_file.sql |
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. |
Found the answer to my own question. The way to do it is:
Code:
mysql -u[user] -p[pass] -D[dbName] << QUERY_INPUT |
All times are GMT -5. The time now is 06:11 AM. |