码迷,mamicode.com
首页 > 数据库 > 详细

shell操作mysql之增删改查

时间:2016-05-15 19:32:41      阅读:231      评论:0      收藏:0      [点我收藏+]

标签:

假设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
#mysql.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 #查

 

shell操作mysql之增删改查

标签:

原文地址:http://www.cnblogs.com/weblm/p/5495810.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!