Linux学习|Gentoo/Arch/FreeBSD

教你用shell来操作mysql

2020-01-20  本文已影响0人  Java及SpringBoot

个人专题目录

1. 教你用shell来连接mysql

#!/bin/bash

#连接数据库
mysql=`which mysql`
#发送单个命令
${mysql} test -h 127.0.0.1 -u root  -P 3306 -proot -e "show databases;"

#发送多个命令
${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输出数据
${mysql} test -h 127.0.0.1 -u root  -P 3306 -proot -X -e 'select * from at'

#使用table标签输出数据
${mysql} test -h 127.0.0.1 -u root  -P 3306 -proot -H -e 'select * from at'

2. 教你用shell来操作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
    #脚本变量一定要用双引号,字符串变量使用单引号
    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
上一篇下一篇

猜你喜欢

热点阅读