php脚本l导出mysq的blob格式数据-hex和unhex的用法

前言

以前咱们介绍过使用PHP脚本导出sql语句到测试服中的流程和注意点, 以前有个问题尚未解决的,就是mysql中blob类型数据是导不成功的。 此次找到了解决方法,这里记录一下。mysql

什么是blob类型?git

咱们在【mysql经常使用数据类型中】介绍过了blob类型,这里也说一下: 在MySQL中,Blob是一个二进制的对象,它是一个能够存储大量数据的容器(如图片,音乐等等),且能容纳不一样大小的数据。 在MySQL中有四种Blob类型,他们的区别就是能够容纳的信息量不容分别是如下四种:sql

①TinyBlob类型 最大能容纳255B的数据数据库

②Blob类型 最大能容纳65KB的json

③MediumBlob类型 最大能容纳16MB的数据函数

④LongBlob类型 最大能容纳4GB的数据测试

场景

咱们的一个表有个字段的数据是存储了一些会员信息,这些信息是由几个字段组成,而后json_encode 而且压缩后存储在字段类型为blob的字段中,这些信息以blob格式存储在数据库中,直接用sql查询出来都是乱码,没法显示,不信你能够尝试查询下。ui

当咱们导出成sql语句时,若是不作特殊处理,这些字段也会是乱码,会致使sql结构破坏掉,不能完整执行。this

解决方案 找了下资料,发现mysql有两个函数,是能够格式化这些二进制数据的。 下面是两个函数的介绍spa

HEX 和UNHEX 建议先看一下官方免费手册中的说明。

HEX(NorS)

If NorS is a number, returns a string representation of the hexadecimal value of N, where N is a longlong (BIGINT) number. This is equivalent to CONV(N,10,16).

翻译:若是NorS是数字,则返回十六进制值N的字符串表示,其中N是longlong(BIGINT)数字。 这至关于CONV(N,10,16)。

If NorS is a string, returns a hexadecimal string representation of NorS where each character in NorS is converted to two hexadecimal digits. The inverse of this operation is performed by the UNHEX() function.

翻译:若是NorS是字符串,则返回NorS的十六进制字符串表示形式,其中NorS中的每一个字符都转换为两个十六进制数字。 此操做的反转由UNHEX()函数执行。

  1. mysql> SELECT HEX(255);
  2. -> 'FF'
  3. mysql> SELECT 0x616263;
  4. -> 'abc'
  5. mysql> SELECT HEX('abc');
  6. -> 616263

UNHEX(str)

Performs the inverse operation of HEX(str). That is, it interprets each pair of hexadecimal digits in the argument as a number and converts it to the character represented by the number. The resulting characters are returned as a binary string.

翻译:执行HEX(str)的逆操做。 也就是说,它将参数中的每对十六进制数字解释为数字,并将其转换为数字表示的字符。 结果字符做为二进制字符串返回。

  1. mysql> SELECT UNHEX('4D7953514C');
  2. -> 'MySQL'
  3. mysql> SELECT 0x4D7953514C;
  4. -> 'MySQL'
  5. mysql> SELECT UNHEX(HEX('string'));
  6. -> 'string'
  7. mysql> SELECT HEX(UNHEX('1267'));
  8. -> '1267'

The characters in the argument string must be legal hexadecimal digits: '0' .. '9', 'A' .. 'F', 'a' .. 'f'. If UNHEX() encounters any nonhexadecimal digits in the argument, it returns NULL:

翻译:参数字符串中的字符必须是合法的十六进制数字:'0'..'9','A'..'F','a'..'f'。 若是UNHEX()在参数中遇到任何非十六进制数字,则返回NULL:

  1. mysql> SELECT UNHEX('GG');
  2. +-------------+
  3. | UNHEX('GG') |
  4. +-------------+
  5. | NULL |
  6. +-------------+

A NULL result can occur if the argument to UNHEX() is a BINARY column, because values are padded with 0x00 bytes when stored but those bytes are not stripped on retrieval. For example 'aa' is stored into a CHAR(3) column as 'aa ' and retrieved as 'aa' (with the trailing pad space stripped), so UNHEX() for the column value returns 'A'. By contrast 'aa' is stored into a BINARY(3) column as 'aa\0' and retrieved as 'aa\0' (with the trailing pad 0x00 byte not stripped). '\0' is not a legal hexadecimal digit, so UNHEX() for the column value returns NULL.

翻译:若是UNHEX()的参数是BINARY列,则可能会出现NULL结果,由于在存储时会使用0x00字节填充值,但这些字节在检索时不会被剥离。 例如,'aa'做为'aa'存储在CHAR(3)列中,并做为'aa'检索(尾随填充空间被剥离),所以列值的UNHEX()返回'A'。 相比之下,'aa'做为'aa \ 0'存储在BINARY(3)列中,并做为'aa \ 0'检索(尾随填充0x00字节未被剥离)。 '\ 0'不是合法的十六进制数字,所以列值的UNHEX()返回NULL。

综上,简单点理解就是

导出时采用HEX函数读取数据,把二进制的数据转为16进制的字符串;

  1. select HEX(binField) from testTable;
  2. //查询出来的数据是一串能够展现的字符串,能够导出为sql语句,
  3. //可是要恢复,要是用其逆转函数UNHEX将数据格式化导入

  1. //导入时采用UNHEX函数,把16进制的字符串转为二进制的数据导入库中;
  2. insert into testTable binField values(UNHEX(@hexstr));
  3. //这样格式化后,能够完整的将数据导进去。
相关文章
相关标签/搜索