Using mysql to store binary data streams _ MySQL

Source: Internet
Author: User
Recently, a project of using mysql to store binary data streams encountered the need to store binary data stream files in DB. it was found that common mysql APIs were useless, fang Zhi has a set of special APIs to do this data, which 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/n ");
Exit (0 );
}
If (mysql_stmt_prepare (stmt, INSERT_QUERY, strlen (INSERT_QUERY )))
{
Fprintf (stderr, "/n mysql_stmt_prepare (), INSERT failed ");
Fprintf (stderr, "/n % s", mysql_stmt_error (stmt ));
Exit (0 );
}
Memset (bind, 0, sizeof (bind ));
// Bind [0]. buffer_type = MYSQL_TYPE_STRING;
// Bind [0]. buffer_type = MYSQL_TYPE_LONG;
Bind [0]. buffer = blog_data;
// Bind [0]. buffer_type = MYSQL_TYPE_TINY;
Bind [0]. buffer_type = MYSQL_TYPE_BLOB;
Bind [0]. length = & length;
Bind [0]. is_null = 0;

/* Bind the buffers */
If (mysql_stmt_bind_param (stmt, bind ))
{
Fprintf (stderr, "/n param bind failed ");
Fprintf (stderr, "/n % s", mysql_stmt_error (stmt ));
Exit (0 );
}

Int rc = 0;
/* Supply data in chunks to server */
If (mysql_stmt_send_long_data (stmt, 0, pos, size ))
{
Fprintf (stderr, "/n send_long_data failed ");
Fprintf (stderr, "/n % s", mysql_stmt_error (stmt ));
Exit (0 );
}

Pos + = size;

/* Supply the next piece of data */
If (mysql_stmt_send_long_data (stmt, 0, pos, size ))
{
Fprintf (stderr, "/n send_long_data failed ");
Fprintf (stderr, "/n % s", mysql_stmt_error (stmt ));
Exit (0 );
}

/* Now, execute the query */
If (mysql_stmt_execute (stmt ))
{
Fprintf (stderr, "/n mysql_stmt_execute failed ");
Fprintf (stderr, "/n % s", mysql_stmt_error (stmt ));
Exit (0 );
}
}


Int main ()
{
Test ();
// Sleep (1 );
Return 0;
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.