LINUX SHELL 操作mysql数据库

linux下shell操作数据库示例:

[root@localhost bill]# cat sqlscript.sh
#!/bin/bash 
 
HOSTNAME="127.0.0.1"                              #数据库信息 
PORT="3306" 
USERNAME="root" 
PASSWORD="" 
 
DBNAME="db_test"                                        #数据库名称 
TABLENAME="table_test"                              #数据库中表的名称 
#创建数据库 
create_db_sql="create database IF NOT EXISTS ${DBNAME}" 
mysql -h${HOSTNAME}  -P${PORT}  -u${USERNAME} -p${PASSWORD} -e "${create_db_sql}" 
 
#创建表 
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} ${DBNAME} -e"${create_table_sql}" 
 
#插入数据 
insert_sql="insert into ${TABLENAME} values('billtan',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}" 

本文出自 “我爱CTO” 博客,谢绝转载!

你可能感兴趣的:(mysql,linux,shell,操作,语句)