如何从 bash 脚本中执行 SQL?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1467846/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How do you execute SQL from within a bash script?
提问by Aaron
I have some SQL scripts that I'm trying to automate. In the past I have used SQL*Plus, and called the sqlplus binary manually, from a bash script.
我有一些试图自动化的 SQL 脚本。过去,我使用过 SQL*Plus,并从 bash 脚本手动调用了 sqlplus 二进制文件。
However, I'm trying to figure out if there's a way to connect to the DB, and call the script from inside of the bash script... so that I can insert date
and make the queries run relative to a certain number of days in the past.
但是,我试图弄清楚是否有办法连接到数据库,并从 bash 脚本内部调用脚本......这样我就可以插入date
并让查询相对于特定天数运行过去。
回答by RC.
I'm slightly confused. You should be able to call sqlplus from within the bash script. This may be what you were doing with your first statement
我有点困惑。您应该能够从 bash 脚本中调用 sqlplus。这可能是你在第一条语句中所做的
Try Executing the following within your bash script:
尝试在 bash 脚本中执行以下操作:
#!/bin/bash
echo Start Executing SQL commands
sqlplus <user>/<password> @file-with-sql-1.sql
sqlplus <user>/<password> @file-with-sql-2.sql
If you want to be able to pass data into your scripts you can do it via SQLPlus by passing arguments into the script:
如果您希望能够将数据传递到您的脚本中,您可以通过将参数传递到脚本中来通过 SQLPlus 来实现:
Contents of file-with-sql-1.sql
内容文件与-SQL 1.SQL
select * from users where username='&1';
Then change the bash script to call sqlplus passing in the value
然后改bash脚本调用sqlplus传入值
#!/bin/bash
MY_USER=bob
sqlplus <user>/<password> @file-with-sql-1.sql $MY_USER
回答by xdhmoore
You can also use a "here document" to do the same thing:
你也可以使用“here document”来做同样的事情:
VARIABLE=SOMEVALUE
sqlplus connectioninfo << HERE
start file1.sql
start file2.sql $VARIABLE
quit
HERE
回答by danadam
Maybe you can pipe SQL query to sqlplus. It works for mysql:
也许您可以将 SQL 查询通过管道传输到 sqlplus。它适用于 mysql:
echo "SELECT * FROM table" | mysql --user=username database
回答by Rondo
I've used the jdbcsql project on Sourceforge.
我在 Sourceforge 上使用了 jdbcsql 项目。
On *nix systems, this will create a csv stream of results to standard out:
在 *nix 系统上,这将创建一个 csv 结果流以进行标准输出:
java -Djava.security.egd=file///dev/urandom -jar jdbcsql.jar -d oracledb_SID -h $host -p 1521 -U some_username -m oracle -P "$PW" -f excel -s "," ""
Note that adding the -Djava.security.egd=file///dev/urandom
increases performance greatly
请注意,添加-Djava.security.egd=file///dev/urandom
大大提高了性能
Windows commands are similar: see http://jdbcsql.sourceforge.net/
Windows 命令类似:参见http://jdbcsql.sourceforge.net/
回答by GeekTantra
Here is a simple way of running MySQL queries in the bash shell
这是在 bash shell 中运行 MySQL 查询的简单方法
mysql -u [database_username] -p [database_password] -D [database_name] -e "SELECT * FROM [table_name]"
回答by Tom Neyland
As Bash doesn't have built in sql database connectivity... you will need to use some sort of third party tool.
由于 Bash 没有内置的 sql 数据库连接......您将需要使用某种第三方工具。