前端之家收集整理的这篇文章主要介绍了
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}"
原文链接:https://www.f2er.com/bash/388101.html