shell处理mysql增、删、改、查

WBOY
Release: 2016-08-08 09:25:19
Original
1228 people have browsed it

引言

    这几天做一个任务,比对两个数据表中的数据,昨天用PHP写了一个版本,但考虑到有的机器没有php或者php没有编译mysql扩展,就无法使用mysql系列的函数,脚本就无效了,今天写个shell版本的,这样,在所有linux系列机器上就都可以运行了。

shell是如何操作mysql的?

    shell操作mysql其实就是通过mysql命令通过参数去执行语句,跟其他程序里面是一样的,看看下面这个参数:

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

因此我们可以通过mysql -e来执行语句,就像下面这样:

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

执行之后返回下面结果:
shell处理mysql增、删、改、查

在shell脚本中操作mysql

导出数据

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
Copy after login

插入数据

#先看看要导入的数据格式,三列,分别是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
Copy after login

输出结果

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

更新数据

#先看看更新数据的格式,将左边一列替换为右边一列,只有左边一列的删除,下面数据放入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
Copy after login

输出结果:

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

本文版权归作者iforever[

]所有,未经作者本人同意禁止任何形式的转载,转载文章之后必须在文章页面明显位置给出作者和原文连接。

以上就介绍了shell处理mysql增、删、改、查,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
source:php.cn
Statement of this Website
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 [email protected]
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!