在命令行里执行mysql的sql
平常执行sql,需要登录到mysql的shell下,然后再执行。比如:
$ /usr/bin/mysql -u root
mysql> select * from users;
但是如果写一点简单的脚本,也可以在命令行下直接运行sql并显示结果,比如:
CODE:
-
$ cat executemysql.sh
-
#!/bin/sh
-
-
qry=$1;
-
-
echo "Executing the following query"
-
echo "$qry"
-
-
mysql -u root <<eof
-
$qry
-
eof
运行一下:
CODE:
-
$ ./executemysql.sh "select id,name,age from test.users limit 2"
-
Executing the following query
-
select id,name,age from test.users limit 2
-
id name age
-
3 john 20
-
4 tom 21
原文:Execute mysql from bash script
作者: Volcano 发表于June 23, 2008 at 11:05 am