5 在批处理模式下使用mysql

96
小_伙
2017.04.07 17:14* 字数 117

在前面的章节中,你交互式地使用mysql输入查询并且查看结果。你也可以以批模式运行mysql。为了做到这些,把你想要运行的命令放在一个文件中,然后告诉mysql从文件读取它的输入:

source filename;

如果在Windows下在命令行上指定连接参数,命令应为:

C:\Users\Administrator>mysql -h localhost -u root -p123456 test -e "source E:/batch.txt"
Enter password: ********

在E盘下新建batch.TXT,里面写入SQL

select * from pet;
select * from pet where name like '%b%';

调用脚本

mysql> source E:/batch.txt(或者 \. E:/batch.txt)
+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Whistler | Gwen  | bird    | NULL | 1997-12-09 | NULL  |
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
2 rows in set (0.00 sec)

+----------+-------+---------+------+------------+-------+
| name     | owner | species | sex  | birth      | death |
+----------+-------+---------+------+------------+-------+
| Puffball | Diane | hamster | f    | 1999-03-30 | NULL  |
+----------+-------+---------+------+------------+-------+
1 row in set (0.00 sec)
MySQL从入门到精通