Welcome to the Linux community forum and interact with 2 million technical staff. Recently, a project encountered the problem of storing binary data stream files in DB. I found that common mysqlapis are useless, further research, Fang zhi has a set of specialized APIs to do this data, which is quite powerful. The following is the sample code-compile and use it according to the instructions,
Welcome to the Linux community forum and interact with 2 million technical staff> recently, a project encountered the problem of storing binary data stream files in DB, if you find that common mysql APIs are useless, you can study them again. It is quite powerful to say that you have a set of special APIs to do this data. The following is the sample code-compile and use it according to the instructions,
Welcome to the Linux community forum and interact with 2 million technicians>
Recently, a project encountered the problem of storing binary data stream files in the database, and found that common mysql apis were not used. After further research, we knew that there was a set of special APIs to do this data, it is quite powerful.
The following is the sample code-compile and use it according to the instructions, and save the binary file with slight modifications.
View plaincopy to clipboardprint?
/*
Mysql database stores binary data linux
Purpose: Use mysql_stmt_send_long_data () to write binary data streams to blob fields.
Note: The buffer_type field of the bind structure must be consistent with the data type to be entered,
For example, if only one long data is written, MYSQL_TYPE_LONG is used to write the bytes stream and MYSQL_TYPE_STRING is used,
Write a binary data stream with MYSQL_TYPE_BLOB
For the meanings of each field of this parameter, see the mysql5.0 manual.
Compile: g ++-I/usr/include/mysql-L/usr/lib/mysql-lmysqlclient mysql_test.cpp
Preparations:
Create database test;
Use test;
Create table 'bintest '(
'Id' int (11) not null default 0,
'Data' blob
) ENGINE = MyISAM;
*/
# Include
# Include
# Include
# Include
# Define INSERT_QUERY "insert into bintest (id, data) VALUES (4 ,?) "
Void test ()
{
MYSQL_BIND bind [1];
Unsigned long length;
Char blog_data [100] = {0 };
Memset (blog_data, 0x01, sizeof (blog_data ));
Char * pos = blog_data;
Int size = 50;
MYSQL * mysql = mysql_init (NULL );
If (! Mysql) return;
If (! Mysql_real_connect (mysql,
"192.168.xx.xxx ",
"Root ",
"Db_user_name ",
"Test ",
3306, NULL, 0 ))
{
Int ret = mysql_errno (mysql );
Mysql_close (mysql );
Return;
}
MYSQL_STMT * stmt = mysql_stmt_init (mysql );
If (! Stmt)
{
Fprintf (stderr, "mysql_stmt_init (), out of memory ");
Exit (0 );
}
If (mysql_stmt_prepare (stmt, INSERT_QUERY, strlen (INSERT_QUERY )))
{
Fprintf (stderr, "mysql_stmt_prepare (), INSERT failed ");
Fprintf (stderr, "% s", mysql_stmt_error (stmt ));
Exit (0 );
}
[1] [2]