在Java中如何 *** 作linux命令行运行.sql文件

在Java中如何 *** 作linux命令行运行.sql文件,第1张

连接linux,
可以使用工具如filezilla ,x5等上传 你的sql文件到linux服务器上
登录mysql
复制代码代码如下:
>mysql -u root -p
>password:
注意,如果你之前建立了其他的mysql用户,可以选择合适的用户登录。
按照下面的三个步骤,快速导入这个sql文件
复制代码代码如下:
mysql>use yourdatabasename;
mysql>set names utf8;
mysql>source /tmp/databasesql;
然后屏幕上就会不断的滚,最后提示导入成功。
最后,记得将databasesql删除。

什么数据库?sqlite?如果是sqlite工具SQLite
Database
Browser打开,或者在终端直接输入命令sqlite3
xxdb,在输入select

from
xxx;
xxdb是数据库,xxx是表名,这样就可以看到xxx表中所有数据了。

因导出sql文件
在你原来的网站服务商处利用phpmyadmin导出数据库为sql文件,这个步骤大家都会,不赘述。
上传sql文件
前面说过了,我们没有在云主机上安装ftp,怎么上传呢?
打开ftp客户端软件,例如filezilla,使用服务器IP和root及密码,连接时一定要使用SFTP方式连接,这样才能连接到linux。注意,这种方法是不安全的,但我们这里没有ftp,如果要上传本地文件到服务器,没有更好更快的方法。
我们把databasesql上传到/tmp目录。
连接到linux,登录mysql
使用putty连接到云主机。putty的使用方法这里就不介绍了。
连接之后,登录mysql
复制代码
代码如下:
>mysql
-u
root
-p
>password:
注意,如果你之前建立了其他的mysql用户,可以选择合适的用户登录。关于mysql的命令行,你可以在其他地方学习。
把上传上去的sql导入到数据库中
按照下面的三个步骤,快速导入这个sql文件
复制代码
代码如下:
mysql>use
yourdatabasename;
mysql>set
names
utf8;
mysql>source
/tmp/databasesql;
然后屏幕上就会不断的滚,最后提示导入成功。
最后,记得将databasesql删除。

同样,可以手动执行sql文件,具体步骤如下:
1、使用root帐户登录到MySQL服务器;
2、执行source命令:
mysql>
source
c:/testsql
注意:文件路径中建议使用“/”,如果使用“\”,要首先进行转义即“\\”,否则可能会出现错误。
另外还有一个load命令可以批量插入数据,但是这个对文件中数据格式的要求比较严格,否则容易出现错误,很少使用。
mysql
查看所有用户的语句
输入指令select
user();
例:(项目来源:尚学堂)struts_training_itemmgrsql内容如下:
Sql代码
DROP
TABLE
t_items;
DROP
TABLE
t_data_dict;
CREATE
TABLE
t_items
(
item_no
varchar(20)
not
null
key,
item_name
varchar(20)
not
null,
spec
varchar(20),
pattern
varchar(10),
category
varchar(20),
unit
char(20)
);
CREATE
TABLE
t_data_dict
(
id
varchar(5)
not
null
key,
category
varchar(20),
name
varchar(30)
);
#
t_data_dict的初始化数据
INSERT
INTO
t_data_dict(id,category,name)
values('B01','item_category','精通Spring2X
Java
Web开发');
INSERT
INTO
t_data_dict(id,category,name)
values('B02','item_category','Java语言与面向对象程序设计');
INSERT
INTO
t_data_dict(id,category,name)
values('B03','item_category','2B铅笔');
INSERT
INTO
t_data_dict(id,category,name)
values('B04','item_category','HOTROCK
notebook');
INSERT
INTO
t_data_dict(id,category,name)
values('C01','item_unit','本');
INSERT
INTO
t_data_dict(id,category,name)
values('C02','item_unit','支');
INSERT
INTO
t_data_dict(id,category,name)
values('C03','item_unit','箱');

工具/原料

mysql   电脑

方法/步骤

1、点击“开始”进入界面,找到mysql。

2、双击打开mysql软件,输入密码。

3、输入“show databases;”即可看到自己创建的数据库。

4、输入“use 数据库名”,使用此数据库。

5、输入“source sql文件的路径”。

6、输入“show tables”,即可看到导入的表了。

1、在gedit中编写sh格式的文件,保存为ash。

2、在终端调用脚本,定位到目录,然后输入。

3、编写的if/else如下:if和 ; 之间的代码ls -l ash是用来判断当前的目录下是否存在ash这个文件。if和else的基本格式如下所示。

4、在终端调用的结果如下,可以看到输出了。

5、再看一个if/else脚本如下脚本,if/else格式和上面格式一致,重点看下 if 和 ;间的那个命令,命令开始以 [,后面有4个参数 $1,=,me,]。

6、输出的结果如下所示。

主要有以下几种方法:
1、将SQL语句直接嵌入到shell脚本文件中
代码如下:
--演示环境
[root@SZDB ~]# more /etc/issue
CentOS release 59 (Final)
Kernel \r on an \m
root@localhost[(none)]> show variables like 'version';
+---------------+------------+
| Variable_name | Value |
+---------------+------------+
| version | 5612-log |
+---------------+------------+

[root@SZDB ~]# more shell_call_sql1sh
#!/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/templog
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/templog>>${LOG}
echo "script executed successful">>${LOG}
exit;

[root@SZDB ~]# /shell_call_sql1sh
Logging to file '/tmp/templog'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled
2、命令行调用单独的SQL文件
代码如下:
[root@SZDB ~]# more tempsql
tee /tmp/templog
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/tempsql"
Logging to file '/tmp/templog'
+------+-------+
| id | val |
+------+-------+
| 1 | jack |
| 2 | robin |
| 3 | mark |
+------+-------+
Outfile disabled
3、使用管道符调用SQL文件
代码如下:
[root@SZDB ~]# mysql -uroot -p123456 </root/tempsql
Logging to file '/tmp/templog'
id val
1 jack
2 robin
3 mark
Outfile disabled
#使用管道符调用SQL文件以及输出日志
[root@SZDB ~]# mysql -uroot -p123456 </root/tempsql >/tmp/templog
[root@SZDB ~]# more /tmp/templog
Logging to file '/tmp/templog'
id val
1 jack
2 robin
3 mark
Outfile disabled
4、shell脚本中MySQL提示符下调用SQL
代码如下:
[root@SZDB ~]# more shell_call_sql2sh
#!/bin/bash
mysql -uroot -p123456 <<EOF
source /root/tempsql;
select current_date();
delete from tempdbtb_tmp where id=3;
select from tempdbtb_tmp where id=2;
EOF
exit;
[root@SZDB ~]# /shell_call_sql2sh
Logging to file '/tmp/templog'
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_sql3sh
#!/bin/bash
cmd="select count() from tempdbtb_tmp"
cnt=$(mysql -uroot -p123456 -s -e "${cmd}")
echo "Current count is : ${cnt}"
exit
[root@SZDB ~]# /shell_call_sql3sh
Warning: Using a password on the command line interface can be insecure
Current count is : 3

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

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

[root@SZDB ~]# /shell_call_sql4sh
Current count is : 1

你需要看一下 args = shlexsplit('~/tools/hive/bin/hive -f test') 输出是什么

shlexsplit('~/tools/hive/bin/hive -f test') 是否和 '~/tools/hive/bin/hive -f test',shell=True 内容一致


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/13357133.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-07-21
下一篇 2023-07-21

发表评论

登录后才能评论

评论列表(0条)

保存