Home  >  Article  >  Backend Development  >  Shell handles mysql addition, deletion, modification and query

Shell handles mysql addition, deletion, modification and query

WBOY
WBOYOriginal
2016-08-08 09:25:191237browse

Introduction

I have been doing a task these days to compare the data in two data tables. I wrote a version using PHP yesterday. However, considering that some machines do not have PHP or PHP does not compile the mysql extension, I cannot use the mysql series of functions and scripts. It is invalid. Today I will write a shell version so that it can run on all Linux series machines.

How does shell operate mysql?

Shell operating mysql is actually to execute statements through parameters through the mysql command, which is the same as in other programs. Take a look at the following parameters:

-e, --execute=name  Execute command and quit. (Disables --force and history file.)

So we can execute the statement through mysql -e, like the following:

mysql -hlocalhost -P3306 -uroot -p123456 $test --default-character-set=utf8 -e "select * from users"

After execution, the following results are returned:
Shell handles mysql addition, deletion, modification and query

Operate mysql in shell script

Export data

MYSQL="mysql -h192.168.1.102 -uroot -p123456 --default-character-set=utf8 -A -N"
#这里面有两个参数,-A、-N,-A的含义是不去预读全部数据表信息,这样可以解决在数据表很多的时候卡死的问题
#-N,很简单,Don't write column names in results,获取的数据信息省去列名称
sql="select * from test.user"
result="$($MYSQL -e "$sql")"

dump_data=./data.user.txt
>$dump_data
echo -e "$result" > $dump_data
#这里要额外注意,echo -e "$result" > $dump_data的时候一定要加上双引号,不让导出的数据会挤在一行

#下面是返回的测试数据
3       吴彦祖  32
5       王力宏  32
6       ab      32
7       黄晓明  33
8       anonymous       32

Insert data

#先看看要导入的数据格式,三列,分别是id,名字,年龄(数据是随便捏造的),放入data.user.txt
12 tf 23
13 米勒 24
14 西安电子科技大学 90
15 西安交大 90
16 北京大学 90

#OLF_IFS=$IFS
#IFS=","
#临时设置默认分隔符为逗号
cat data.user.txt | while read id name age
do
	sql="insert into test.user(id, name, age) values(${id}, '${name}', ${age});"
	$MYSQL -e "$sql"
done

Output results

+----+--------------------------+-----+
| id | name                     | age |
+----+--------------------------+-----+
| 12 | tf                       |  23 |
| 13 | 米勒                   	|  24 |
| 14 | 西安电子科技大学 	|  90 |
| 15 | 西安交大             	|  90 |
| 16 | 北京大学             	|  90 |
+----+--------------------------+-----+

Update data

#先看看更新数据的格式,将左边一列替换为右边一列,只有左边一列的删除,下面数据放入update.user.txt
tf twoFile
西安电子科技大学 西军电
西安交大 西安交通大学
北京大学

cat update.user.txt | while read src dst
do
	if [ ! -z "${src}" -a ! -z "${dst}" ]
	then
		sql="update test.user set name='${dst}' where name='${src}'"
	fi
	if [ ! -z "${src}" -a -z "${dst}" ]
	then
		sql="delete from test.user where name='${src}'"
	fi
	$MYSQL -e "$sql"
done

Output result:

+----+--------------------------+-----+
| id | name                     | age |
+----+--------------------------+-----+
| 12 | twoFile                  |  23 |
| 13 | 米勒                   	|  24 |
| 14 | 西军电		 	|  90 |
| 15 | 西安交通大学         	|  90 |
+----+--------------------------+-----+

The copyright of this article belongs to the author iforever [

]. Any form of reprinting is prohibited without the consent of the author. After reprinting the article, the author and the original text link must be given in an obvious position on the article page.

The above introduces the shell processing of mysql addition, deletion, modification, and query, including aspects of the process. I hope it will be helpful to friends who are interested in PHP tutorials.

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn