MySQL中如何插入blob类型数据

原创 2007年12月25日 18:57:00

等具体研究完写篇完整的,现在只是作为一个标记,以免忘记mysql_real_escape_string函数。

mysql_real_escape_string()

unsigned long mysql_real_escape_string(MYSQL *mysql, char *to, const char *from, unsigned long length)

Note that mysql must be a valid, open connection. This is needed because the escaping depends on the character set in use by the server.

Description

This function is used to create a legal SQL string that you can use in an SQL statement. See Section 9.1.1, “Strings”.

The string in from is encoded to an escaped SQL string, taking into account the current character set of the connection. The result is placed in to and a terminating null byte is appended. Characters encoded are NUL (ASCII 0), ‘/n’, ‘/r’, ‘/’, ‘'’, ‘"’, and Control-Z (see Section 9.1, “Literal Values”). (Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in log files.)

The string pointed to by from must be length bytes long. You must allocate the to buffer to be at least length*2+1 bytes long. (In the worst case, each character may need to be encoded as using two bytes, and you need room for the terminating null byte.) When mysql_real_escape_string() returns, the contents of to is a null-terminated string. The return value is the length of the encoded string, not including the terminating null character.

If you need to change the character set of the connection, you should use the mysql_set_character_set() function rather than executing a SET NAMES (or SET CHARACTER SET) statement. mysql_set_character_set() works like SET NAMES but also affects the character set used by mysql_real_escape_string(), which SET NAMES does not.

Example

char query[1000],*end;

end = strmov(query,"INSERT INTO test_table values(");
*end++ = '/'';
end += mysql_real_escape_string(&mysql, end,"What's this",11);
*end++ = '/'';
*end++ = ',';
*end++ = '/'';
end += mysql_real_escape_string(&mysql, end,"binary data: /0/r/n",16);
*end++ = '/'';
*end++ = ')';

if (mysql_real_query(&mysql,query,(unsigned int) (end - query)))
{
   fprintf(stderr, "Failed to insert row, Error: %s/n",
           mysql_error(&mysql));
}

The strmov() function used in the example is included in the mysqlclient library and works like strcpy() but returns a pointer to the terminating null of the first parameter.

Return Values

The length of the value placed into to, not including the terminating null character.

Errors

None.

 

MySQL 中Blob类型数据的插入和读取

在MySQL中Blob是一个二进制的对象,它是一个可以存储大量数据的容器(如图片,音乐等等),且能容纳不同大小的数据,在MySQL中有四种Blob类型,他们的区别就是可以容纳的信息量不容分别是以下四种...
  • qq_38785658
  • qq_38785658
  • 2017-07-12 13:55:42
  • 9266

MySQL的BLOB类型

BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型。BLOB是一 个大文件,典型的...
  • tashanhongye
  • tashanhongye
  • 2015-10-14 20:36:24
  • 5219

MySQL Blob类型简介

BLOB类型的字段用于存储二进制数据 MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小...
  • u010159842
  • u010159842
  • 2016-09-14 15:29:50
  • 884

mysql的BLOB类型

1、BLOB类型 1、BLOB类型介绍 (1)BLOB类型的含义 BLOB (binary large object),用来存储二进制大对象的字段类型。 BLOB往往是一个大文件,典型的BLOB是一张...
  • jiayichendddd
  • jiayichendddd
  • 2014-12-18 14:53:44
  • 3665

Navicat for MySQL 查看BLOB字段内容

Navicat for MySQL 查看BLOB字段内容查看BLOB内容的两种方法:   1. 在查询窗口中选中BLOB字段,点击备注   2.  查看-》原始数据模式...
  • lwei_998
  • lwei_998
  • 2014-12-11 20:04:54
  • 59006

mysql sql 语句插入 blob字段的插入

1 HEX 函数转换为 将一个字符串或数字转换为十六进制格式的字符串 2 使用 UNHEX()  把十六进制格式的字符串转化为原来的格式 使用实例 insert into tb_test(blo...
  • yujun_huoxu
  • yujun_huoxu
  • 2015-05-27 11:19:59
  • 9213

MySQL中BLOB字段类型介绍

转自:http://www.php100.com/html/webkaifa/database/Mysql/2010/1126/6900.html BLOB类型的字段用于存储二进制数据MySQL中,...
  • qwq1503
  • qwq1503
  • 2016-10-30 15:06:16
  • 1815

Mysql学习篇之---四种Blob类型

Mysql学习篇之---四种Blob类型 BLOB类型的字段用于存储二进制数据,BLOB是一个二进制大对象,可以容纳可变数量的数据 MySQL中,BLOB是个类型系列,包括:TinyBlob、Bl...
  • A_little_e
  • A_little_e
  • 2015-11-10 15:07:04
  • 3453

MySQL BLOB类型数据操作

一、MySQL BLOB 类型介绍MySQL中,BLOB是一个二进制大型对象,是一个可以存储大量数据的容器,它能容纳不同大小的数据。BLOB类型实际是个类型系列(TinyBlob、Blob、Mediu...
  • guhong5153
  • guhong5153
  • 2011-04-20 13:10:00
  • 15902

高性能mysql之慎用BLOB与TEXT

高性能mysql之慎用BLOB与TEXT
  • john1337
  • john1337
  • 2017-04-28 16:59:13
  • 3459
收藏助手
不良信息举报
您举报文章:MySQL中如何插入blob类型数据
举报原因:
原因补充:

(最多只允许输入30个字)