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.

 

相关文章推荐

Navicat for MySQL 查看BLOB字段内容

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

mysql的BLOB类型

1、BLOB类型 1、BLOB类型介绍 (1)BLOB类型的含义 BLOB (binary large object),用来存储二进制大对象的字段类型。 BLOB往往是一个大文件,典型的BLOB是一张...

MySQL中BLOB字段类型介绍

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

MySQL下,text 、blob的比较

MySQL存在text和blob:(1)相同在TEXT或BLOB列的存储或检索过程中,不存在大小写转换,当未运行在严格模式时,如果你为BLOB或TEXT列分配一个超过该列类型的最大长度的值值,值被截取...

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

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

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

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

mysql blob

什么是blob,mysql blob大小配置 2010-04-06 17:21 BLOB介绍 BLOB (binary large object),二进制大对象,是一个可以存储二进制...

MySql中Blob字段详解

BLOB类型的字段用于存储二进制数据 MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小上不...

MySQL的BLOB类型

BLOB (binary large object),二进制大对象,是一个可以存储二进制文件的容器。在计算机中,BLOB常常是数据库中用来存储二进制文件的字段类型。BLOB是一 个大文件,典型的...

MySQL Blob类型简介

BLOB类型的字段用于存储二进制数据 MySQL中,BLOB是个类型系列,包括:TinyBlob、Blob、MediumBlob、LongBlob,这几个类型之间的唯一区别是在存储文件的最大大小...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:MySQL中如何插入blob类型数据
举报原因:
原因补充:

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