個(gè)人專(zhuān)題目錄
?
1. 教你用shell來(lái)連接mysql
#!/bin/bash
#連接數(shù)據(jù)庫(kù)
mysql=`which mysql`
#發(fā)送單個(gè)命令
${mysql} test -h 127.0.0.1 -u root -P 3306 -proot -e "show databases;"
#發(fā)送多個(gè)命令
${mysql} test -h 127.0.0.1 -u root -P 3306 -proot <<EOF
show tables;
select * from at;
EOF
dbs=`${mysql} test -h 127.0.0.1 -u root -P 3306 -proot -Bse 'show tables;'`
for db in ${dbs}
do
echo ${db}
done
#使用xml輸出數(shù)據(jù)
${mysql} test -h 127.0.0.1 -u root -P 3306 -proot -X -e 'select * from at'
#使用table標(biāo)簽輸出數(shù)據(jù)
${mysql} test -h 127.0.0.1 -u root -P 3306 -proot -H -e 'select * from at'
2. 教你用shell來(lái)操作mysql
#!/bin/bash
# send data to the the table in the MYSQL database
MYSQL=`which mysql`
if [[ $# -ne 2 ]]
then
echo "must be 2 params"
else
#腳本變量一定要用雙引號(hào)忱详,字符串變量使用單引號(hào)
statement=" insert into at values('$1', $2)"
${MYSQL} test -h 127.0.0.1 -u root -P 3306 -proot <<EOF
${statement}
EOF
if [[ $? -eq 0 ]]
then
echo Data successfully added
else
echo Problem adding data
fi
fi