shell操作mysql之增删改查

假设mysql用户名root 密码123456,新建测试数据表utable 脚本如下:

#!/bin/bash
#mysqlop.sh
mysql="/app/local/mysql/bin/mysql -uroot -p123456"
#sql="show databases"
#sql="show tables from test"
sql="create table test.utable(
 id int unsigned auto_increment primary key,
 username varchar(50),
 passwd varchar(50)
)"
#sql="desc test.utable"
#sql="insert into test.utable(username,passwd) values('user3',321)"
#sql="select * from test.utable"
#sql="delete from test.utable where id=3"
#sql="update test.utable set passwd=135 where id=2"
#sql="drop table test.utable"

$mysql -e "$sql"

#sh mysqlop.sh

新建完成后需要注释掉创建语句,继续编辑脚本

#!/bin/bash
#mysqlop.sh
mysql="/app/local/mysql/bin/mysql -uroot -p123456"

case $1 in
select)
sql="select * from test.utable"
;;
delete)
sql="delete from test.utable where id=$2"
;;
insert)
sql="insert into test.utable(username,passwd)values('$2','$3')"
;;
update)
sql="update test.utable set username='$3',passwd=$4 where id='$2'"
;;
*)
sql="select * from test.utable"
;;
esac

$mysql -e "$sql"

:x保存退出

#chmod +x mysqlop.sh

调用示例如下:

#./mysqlop.sh insert user1 321#增

#./mysqlop.sh insert user2 312#增

#./mysqlop.sh insert user3 123#增

#./mysqlop.sh insert user4 132#增

#./mysqlop.sh insert user5 213#增

#./mysqlop.sh delete  5#删

#./mysqlop.sh update 2 user6 111#改

#./mysqlop.sh select#查

#./mysqlop.sh aaa #查

上一篇:关于QT和SQLite以及XML


下一篇:[置顶] myEclipse8.5或者eclipse手工安装jd插件(myEclipse8.5或eclipse内直接查看.class文件,jd反编译工具)