Shell基础:Shell和Mysql交互

通过命令行和Mysql交互

[root]#mysql -uroot -p123 -e "show databases"  

-e: execute: 执行数据库命令

通过脚本和数据库进行交互,sql一定要用""引起来表示执行语句

#!/bin/bash

#mysql.sh

mysql="/usr/local/mysql/bin/mysql -uroot -p123"

sql="show databases"

$mysql -e "$sql"

运行上述脚本即可实现数据库查询

通过脚本实现参数化查询

#!/bin/bash

#mysql.sh

conn="/usr/local/mysql/bin/mysql -uroot -p123"

case $1 in

   select)

        sql="select * from test.user"

        ;;

  delete)

       sql="delete from test.user where id =$2"

      ;;

 insert)

      sql="insert into test.user(uname,psd) values('$2','$3')"

     ;;

 update)

     sql="update test.user set uname='$3', psd='$4' where id='$2'

    ;;

esac

$conn -e "$sql"

 

 

posted @ 2016-08-04 23:46  heartGoOn99  阅读(1121)  评论(0编辑  收藏  举报