-- 引子--
由于调试需要,需直接往数据库里写入二进制数据。本来这些数据是由上层软件来写的,用的是C#。为了熟悉C语言的数据库操作,还是决定用C来写这段调试代码。
概况:
表名:Task
涉及的字段及属性:
NumDest:int(11) 用于存储目标数目
destIDs: blob 用于存储具体的目标ID
废话不多说,入正题。
--二进制数据写入--
二进制数据最为常见的就是图片等一些文件信息。虽然我这里不是这类型信息,但确实是二进制数据。
具体步骤:
1、 定义一个buffer(如数组)来存储sql语句
2、 把涉及到二进制数据之前的sql语句添加到buffer中,可用sprintf或strcpy等。
3、 用mysql_real_escape_string()函数添加二进制数据到buffer中。
4、 加上剩余的sql语句,形成完整的sql语句。
5、 利用mysql_real_query()函数来执行sql语句。
具体代码如下:
#include <stdio.h> #include <stdlib.h> #include <mysql/mysql.h> #include <stdint.h> #include <string.h> int main(int argc, char *argv[]) { MYSQL mysql; char sql[256], *end; int index, i; uint32_t *destIDs; if(argc != 2) { printf("enter error!\n"); exit(1); } index = atoi(argv[1]); printf("index: %d\n", index); destIDs = (uint32_t *)malloc(index * sizeof(uint32_t)); if(destIDs == NULL) printf("malloc error\n"); for(i=0; i<index; i++) destIDs[i] = i + 1; mysql_init(&mysql); if(!(mysql_real_connect(&mysql, "localhost", "root", "654321", "dbname", 0, NULL, 0))) { fprintf(stderr, "Couldn't connect to engine!\n%s\n", mysql_error(&mysql)); perror(""); exit(1); } sprintf(sql, "INSERT INTO Task(NumDest, DestIDs) VALUE (%u, ", index ); end = sql + strlen(sql); *end++ = '\''; end += mysql_real_escape_string(&mysql, end,(char *)destIDs, index*sizeof(uint32_t)); *end++ = '\''; *end++ = ')'; printf("end - sql: %d\n", (unsigned int)(end - sql)); if(mysql_real_query(&mysql, sql, (unsigned int)(end - sql))) { fprintf(stderr, "Query failed (%s)\n", mysql_error(&mysql)); exit(1); } mysql_close(&mysql); exit(0); #endif return 0; } |
--读取二进制文件--
对于二进制文件的读取,也类似。
具体步骤:
1,构造查询字串.
2,执行mysql _query查询. (网上有说用mysql_real_query,未实验)
3,用mysql_store_result存储结果.
4,用mysql_fetch_row取出一条记录处理.
具体代码如下:
#include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <mysql/mysql.h> #include <string.h> int main(void) { int ret, i; char sql[256]; MYSQL mysql; MYSQL_RES *result; MYSQL_ROW row; uint32_t *destIDs, *temp; unsigned int destNum = 0; mysql_init(&mysql); if(!(mysql_real_connect(&mysql, "localhost", "root", "654321", "dbname", 0, NULL, 0))) { fprintf(stderr, "Couldn't connect to engine!\n%s\n", mysql_error(&mysql)); perror(""); exit(1); } sprintf(sql, "SELECT TaskID, NumDest, DestIDs FROM Task"); ret = mysql_query(&mysql, sql); if(ret != 0) { printf( "Failed to query task table: %s\n", mysql_error(&mysql)); return ret; } result = mysql_store_result(&mysql); if(result == NULL) { ret = mysql_errno(&mysql); printf( "Failed to store query result from task table:%s\n", mysql_error(&mysql)); return ret; } if((row = mysql_fetch_row(result)) != NULL) { sscanf(row[1], "%u", &destNum); destIDs = (uint32_t *)malloc(destNum * sizeof(uint32_t)); if(destIDs == NULL) { printf("malloc error!\n"); exit(1); } memcpy(destIDs, row[2], destNum * sizeof(uint32_t)); } mysql_free_result(result); printf("destNum: %d\n", destNum); temp = destIDs; for(i=0; i<destNum; i++) { printf("destIDs[%d]:%d\t", i+1, *temp++); } return ret; } |
由于我这里可以根据NumDest获取到二进制的长度,所以不用再用函数去获取。
据网上信息,获取二进制信息长度应该这样:“如果取出来的是二进制的数据,要确定它的长度,必须要用mysql_fetch_lengths函数取得其长度”
int num_fields = mysql_num_fields(result); unsigned long *lengths = mysql_fetch_lengths(result); for(i=0; i<num_fields; i++) printf("Column: %u\t %lu bytes\n", i+1, lengths[i]); destIDs = (uint32_t *)malloc(lengths[2]); if(destIDs == NULL) { printf("malloc error!\n"); exit(1); } memcpy(destIDs, row[2], lengths[2]); |
取二进制数据:
一样的sql语句,查询出来即可。只不过二进制数据是个数据块,需要得到数据块的大小和数据指针。
bool CMySqlAccess::GetBinaryField(int nCol,char* &pDataOut,int& nDataLen)
{
if (m_ItemMySqlRow[nCol] != NULL)
{
unsigned long *FieldLength = mysql_fetch_lengths(m_pMySqlResult);
nDataLen = (int)FieldLength[nCol];
pDataOut = (char*)(m_ItemMySqlRow[nCol]);
return true;
}
else
{
return false;
}
}
像通常一样查询后,得到结果集,然后得到第nCol列结果,返回二进制指针结果和二进制长度。返回后必须立马处理或者存储一份。否则mysql将数据销毁,指针所指数据则无效了。
存二进制数据:
mysql语句接受的sql语句都是string,以'\0'结尾的。如果冒然插入二进制数据到sql语句中,要么报错,要么存储错误。此处可以通过mysql提供的函数将数据转换一下即可。
char* CMySqlAccess::ConvertBinaryToString(char* pBinaryData,int nLen)
{
static char s_BinaryData[10240];
mysql_real_escape_string(m_pMySqlConn,s_BinaryData,pBinaryData,nLen);
return s_BinaryData;
}
上面这个函数只能单线程使用啊,将一块二进制数据转换为mysql可识别的string数据。这样就直接可以通过mysql的sql语句insert,update来对blob数据进行更新和插入了,sql语句用法不变。
用例:
std::ostringstream strSQL;
strSQL<<"INSERT INTO "<<m_strTableName<<"(roleid,playerdata,dynamicdata) VALUES("<<dwDBRoleID
<<",'"<<m_pDBAccess->ConvertBinaryToString(pData,nLen)<<"','')";
assert(m_pDBAccess);
m_pDBAccess->ExecuteSQL(strSQL.str());
playerdata是blob二进制类型,pData是指向一个结构体的指针,nLen是结构体的大小。
上面就可以实现二进制的存储了。
方法二:
上面的方法,你会发现,你每次都需要转换数据,传指针,传大小等一系列复杂操作,是不是顺序很混乱,过程很繁杂。mysql也为你提供了另外一种方法,那就是MYSQL_BIND。将数据操作统一化,统一麻烦化。mysqlbind是一个结构体,根据个人不同需求填充各个数据成员可以存储任意类型数据,当然包括blob。
bool CMySqlAccess::SetBinaryField(std::string& strCondition,void* pDataIn,int nDataLen)
{
if( ! mysql_stmt_prepare( m_pMySqlStmt, strCondition.c_str(), strCondition.length() ) )
{
memset(&m_MySqlBind,0,sizeof(MYSQL_BIND));
m_MySqlBind.buffer_type = MYSQL_TYPE_BLOB;
(*m_MySqlBind.length) = nDataLen;
memcpy(m_MySqlBind.buffer,pDataIn,nDataLen);
if(!mysql_stmt_bind_param(m_pMySqlStmt, (MYSQL_BIND *)&m_MySqlBind))
{
if(!mysql_stmt_execute(m_pMySqlStmt))
{
return true;
}
}
}
int nRes=GetDBErrorCode();
CLogOutStream errLog(crazy::ERROR_LEVEL,THIS_CLASS_NAME);
errLog<<"MySql Query Failed:\""<<strCondition<<"\" ,ErrorCode:"<<nRes<<crazy::logEnd;
return false;
}
这个是对某一列blob数据进行存操作。pDataIn和nDataLen分别是一个struct结构体和结构体大小。填充完毕mysqlbind之后即可对数据库二进制列进行存储了。可能你会问,没有指定哪一列呢,对。哪一列是在strCondition语句里面的,这是一个预处理语句。在预处理语句里面,有一个符号: ? 。问号,问号的位置代表了mysqlbind数据对应的位置。
INSERT INTO test_table(date_field, time_field, timestamp_field) VALUES(?,?,?)
上面这个语句,有3个问号,三个问号分别对应test_table的三列.每个问号呢又对应一个mysqlbind数据结构。那么我们在mysql_stmt_bind_param函数调用时,就应该传入一个mysql_bind 数组。MYSQL_BIND m_MySqlBind[3].
填充整个数组数据,即对应三个问号内容。
用例:
MYSQL_BIND bind[3];
MYSQL_STMT *stmt;
strmov(query, "INSERT INTO test_table(date_field, time_field, timestamp_field) VALUES(?,?,?");
//初始化stmt
stmt = mysql_stmt_init(mysql);
//预处理语句
mysql_stmt_prepare(mysql, query, strlen(query));
//初始化参数
bind[0].buffer_type= MYSQL_TYPE_DATE;
bind[0].buffer= (char *)&ts;
bind[0].is_null= 0;
bind[0].length= 0;
bind[1]= bind[2]= bind[0];
//绑定参数123
mysql_stmt_bind_param(stmt, bind);
//执行预处理mysql语句
mysql_stmt_execute(stmt);
还没看懂就个人去看mysql文档了,其实里面讲得很清楚,只要找对几个函数,就可以把search出来了
转自:http://blog.chinaunix.net/uid-23842323-id-2656614.html
Reference:http://topic.csdn.net/u/20090316/11/ac003f13-d1da-49a5-b12f-90e57cbe5ac9.html
FROM: http://www.cppblog.com/Daywei0123/archive/2012/07/05/181479.html
================================================================================
Inserting images into MySQL database
Some people prefer to put their images into the database, some prefer to keep them on the file system for their applications. Technical difficulties arise when we work with millions of images. Images are binary data. MySQL database has a special data type to store binary data called BLOB (Binary Large Object).
mysql> describe images;
+-------+------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+------------+------+-----+---------+-------+
| id | int(11) | NO | PRI | | |
| data | mediumblob | YES | | NULL | |
+-------+------------+------+-----+---------+-------+
2 rows in set (0.00 sec)
This is the table, that we will use in our example. It can be created by the following SQL statement.
create table images(id int not null primary key, data mediumblob);
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *conn;
int len, size;
char data[1000*1024];
char chunk[2*1000*1024+1];
char query[1024*5000];
FILE *fp;
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);
fp = fopen("image.png", "rb");
size = fread(data, 1, 1024*1000, fp);
mysql_real_escape_string(conn, chunk, data, size);
char *stat = "INSERT INTO images(id, data) VALUES('1', '%s')";
len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat, chunk);
mysql_real_query(conn, query, len);
fclose(fp);
mysql_close(conn);
}
In this example, we will insert one image into the images table. The image can be max 1 MB.
fp = fopen("image.png", "rb");
size = fread(data, 1, 1024*1000, fp);
Here we open the image and read it into the data array.
mysql_real_escape_string(conn, chunk, data, size);
Binary data can obtain special characters, that might cause troubles in the statements. We must escape them. The mysql_real_escape_string()puts the encoded data into the chunk array. In theory, every character might be a special character. That's why the chunk array two times as big as the data array. The function also adds a terminating null character.
char *stat = "INSERT INTO images(id, data) VALUES('1', '%s')";
len = snprintf(query, sizeof(stat)+sizeof(chunk) , stat, chunk);
These two code lines prepare the MySQL query.
mysql_real_query(conn, query, len);
Finally, we execute the query.
Selecting images from MySQL database
In the previous example, we have inserted an image into the database. In the following example, we will select the inserted image back from the database.
#include <my_global.h>
#include <mysql.h>
int main(int argc, char **argv)
{
MYSQL *conn;
MYSQL_RES *result;
MYSQL_ROW row;
unsigned long *lengths;
FILE *fp;
conn = mysql_init(NULL);
mysql_real_connect(conn, "localhost", "zetcode", "passwd", "testdb", 0, NULL, 0);
fp = fopen("image.png", "wb");
mysql_query(conn, "SELECT data FROM images WHERE id=1");
result = mysql_store_result(conn);
row = mysql_fetch_row(result);
lengths = mysql_fetch_lengths(result);
fwrite(row[0], lengths[0], 1, fp);
mysql_free_result(result);
fclose(fp);
mysql_close(conn);
}
In this example, we will create an image file from the database.
fp = fopen("image.png", "wb");
We open a file for writing.
mysql_query(conn, "SELECT data FROM images WHERE id=1");
We select an image with id 1.
row = mysql_fetch_row(result);
The row contains raw data.
lengths = mysql_fetch_lengths(result);
We get the length of the image.
fwrite(row[0], lengths[0], 1, fp);
We create the image file using the fwrite() standard function call.
FROM: http://blog.chinaunix.net/uid-18989614-id-2798644.html