Shell(シェルスクリプト)でMySQLの作成、テーブルの作成、挿入、削除操作方法

参考コード:
#!/bin/bash

HOSTNAME="XXX.XXX.XXX.157″
PORT="3306″
USERNAME="root"
PASSWORD="888888″
DBNAME="cftdbinfo"
TABLENAME="cfttblname"

createdbSQL="create database IF NOT EXISTS ${DBNAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} -e “${createdbSQL}"
#テーブルの作成
createTblSQL="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 “${createTblSQL}"

#データの挿入
insertSQL="insert into ${TABLENAME} values('shimara’,2)"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${insertSQL}"

#データのクエリ
selectSQL="select * from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${selectSQL}"

#データを更新
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 “${selectSQL}"

#データの削除
deleteSQL="delete from ${TABLENAME}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${deleteSQL}"
mysql -h${HOSTNAME} -P${PORT} -u${USERNAME} -p${PASSWORD} ${DBNAME} -e “${selectSQL}"

shellscript

Posted by arkgame