linux系统下怎么在终端运行sql语句

 我来答
lijingsandy
2015-08-18 · TA获得超过2.4万个赞
知道小有建树答主
回答量:1734
采纳率:93%
帮助的人:187万
展开全部
主要有以下几种方法:
1、将SQL语句直接嵌入到shell脚本文件中
代码如下:

--演示环境
[root@SZDB ~]# more /etc/issue
CentOS release 5.9 (Final)
Kernel \r on an \m
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5.6.12-log |
+---------------+------------+

[root@SZDB ~]# more shell_call_sql1.sh
#!/bin/bash
# Define log
TIMESTAMP=`date +%Y%m%d%H%M%S`
LOG=call_sql_${TIMESTAMP}.log
echo "Start execute sql statement at `date`." >>${LOG}

# execute sql stat
mysql -uroot -p123456 -e "
tee /tmp/temp.log
drop database if exists tempdb;
create database tempdb;
use tempdb
create table if not exists tb_tmp(id smallint,val varchar(20));
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');
select * from tb_tmp;
notee
quit"

echo -e "\n">>${LOG}
echo "below is output result.">>${LOG}
cat /tmp/temp.log>>${LOG}
echo "script executed successful.">>${LOG}
exit;

[root@SZDB ~]# ./shell_call_sql1.sh
Logging to file '/tmp/temp.log'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled.

2、命令行调用单独的SQL文件

代码如下:

[root@SZDB ~]# more temp.sql
tee /tmp/temp.log
drop database if exists tempdb;
create database tempdb;
use tempdb
create table if not exists tb_tmp(id smallint,val varchar(20));
insert into tb_tmp values (1,'jack'),(2,'robin'),(3,'mark');
select * from tb_tmp;
notee
[root@SZDB ~]# mysql -uroot -p123456 -e "source /root/temp.sql"
Logging to file '/tmp/temp.log'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled.

3、使用管道符调用SQL文件
代码如下:

[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql
Logging to file '/tmp/temp.log'
id val
1 jack
2 robin
3 mark
Outfile disabled.
#使用管道符调用SQL文件以及输出日志
[root@SZDB ~]# mysql -uroot -p123456 </root/temp.sql >/tmp/temp.log
[root@SZDB ~]# more /tmp/temp.log
Logging to file '/tmp/temp.log'
id val
1 jack
2 robin
3 mark
Outfile disabled.

4、shell脚本中MySQL提示符下调用SQL

代码如下:

[root@SZDB ~]# more shell_call_sql2.sh
#!/bin/bash
mysql -uroot -p123456 <<EOF
source /root/temp.sql;
select current_date();
delete from tempdb.tb_tmp where id=3;
select * from tempdb.tb_tmp where id=2;
EOF
exit;
[root@SZDB ~]# ./shell_call_sql2.sh
Logging to file '/tmp/temp.log'
id val
1 jack
2 robin
3 mark
Outfile disabled.
current_date()
2014-10-14
id val
2 robin

5、shell脚本中变量输入与输出

代码如下:

[root@SZDB ~]# more shell_call_sql3.sh
#!/bin/bash
cmd="select count(*) from tempdb.tb_tmp"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit
[root@SZDB ~]# ./shell_call_sql3.sh
Warning: Using a password on the command line interface can be insecure.
Current count is : 3

[root@SZDB ~]# echo "select count(*) from tempdb.tb_tmp"|mysql -uroot -p123456 -s
3

[root@SZDB ~]# more shell_call_sql4.sh
#!/bin/bash
id=1
cmd="select count(*) from tempdb.tb_tmp where id=${id}"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit

[root@SZDB ~]# ./shell_call_sql4.sh
Current count is : 1
匿名用户
推荐于2017-12-15
展开全部
[oracle@ls ~]$ echo "select count(*) from tab;" | sqlplus -s sys/oracle as sysdba  
  
  COUNT(*)  
----------  
      4750
当然为了熟悉语句,练习的话,也可以直接 运行mysql 然后自己练习就好了
本回答被提问者和网友采纳
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
那段逝去的过往
2015-09-05 · TA获得超过472个赞
知道小有建树答主
回答量:458
采纳率:83%
帮助的人:325万
展开全部
  1. 使用终端连接工具连接进入系统当中

  2. 使用命令mysql -u 用户名 -p 密码进入到mysql当中

  3. 连接成功后即可使用sql语句来进行相关的操作

已赞过 已踩过<
你对这个回答的评价是?
评论 收起
梭罗瓦尔登
2015-08-26 · TA获得超过176个赞
知道小有建树答主
回答量:261
采纳率:50%
帮助的人:176万
展开全部
首先你得安装mysql、sqlite等数据库才行,这样才能运行sql
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
hustnzj2010
2015-01-27 · TA获得超过424个赞
知道小有建树答主
回答量:415
采纳率:0%
帮助的人:341万
展开全部
登录sqlplus就可以了。。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
收起 更多回答(3)
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式