shell实现对mysql数据库的增、删、改、查操作

shell实现对MysqL数据库的增、删、改、查操作

#!/bin/bash 
 #数据库信息
HOSTNAME="192.168.111.84"                                          
PORT="3306"
USERNAME="root"
PASSWORD=""
dbnAME="test_db_test"                                                      
TABLENAME="test_table_test"                                           
#也可以写 HOSTNAME="localhost",端口号 PORT可以不设定

 #创建数据库
create_db_sql="create database IF NOT EXISTS ${dbnAME}"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}"

注意:-p${PASSWORD}中间不能有空格

#创建表
create_table_sql="create table IF NOT EXISTS ${TABLENAME} (  name varchar(20),id int(11) default 0 )"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD}  -D ${dbnAME} -e "${create_db_sql}"

#插入数据
insert_sql="insert into ${TABLENAME} values('billchen',2)"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e  "${insert_sql}"

#查询
select_sql="select * from ${TABLENAME}"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e "${select_sql}"

#更新数据
update_sql="update ${TABLENAME} set id=3"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e "${update_sql}"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e  "${select_sql}"

#删除数据
delete_sql="delete from ${TABLENAME}"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e  "${delete_sql}"
MysqL -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} ${dbnAME} -e  "${select_sql}"

相关文章

用的openwrt路由器,家里宽带申请了动态公网ip,为了方便把2...
#!/bin/bashcommand1&command2&wait从Shell脚本并行...
1.先查出MAMP下面集成的PHP版本cd/Applications/MAMP/bin/ph...
1、先输入locale-a,查看一下现在已安装的语言2、若不存在如...
BashPerlTclsyntaxdiff1.进制数表示Languagebinaryoctalhexa...
正常安装了k8s后,使用kubect工具后接的命令不能直接tab补全...