MysqlLONGBLOB storage binary data (modification + debugging + sorting) _ MySQL

Source: Internet
Author: User
MysqlLONGBLOB stores binary data (modification + debugging + sorting) bitsCN.com creates a database test, table bintest, and data field data type in the middle of the DBMS to be tested using LONGBLOB.
// Test file c: // test. iso. you can find any file and change it to an exe program and test. iso.
// The maximum size of files added to the test is 650 MB (a genuine iso file)
// Note: You must also modify the max_allowed_packet field in the my. ini file. what I set is

// Max_allowed_packet = 1024 M
// # Define host "localhost" // mysql server
// # Define username "root"
// # Define password "674800"
// # Define database "test"
// Int port = 3306;
// Mysql3.cpp: Defines the entry point for the console application.
//
# Include "stdafx. h"
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Pragma comment (lib, "libmysql. lib ")
# Define INSERT_QUERY "insert into bintest (id, data) VALUES (NULL ,?) "
# Define host "localhost" // mysql server
# Define username "root"
# Define password "674800"
# Define database "test"
Int port = 3306;
Int get_file_size (char * path, off_t * size)
{
Struct stat file_stats;
If (stat (path, & file_stats ))
Return-1;
* Size = file_stats.st_size;
Return 0;
}
Void test ()
{
MYSQL_BIND bind [1];
Unsigned long length;
Char * pos = NULL;
Off_t size;
FILE * fp;
Char * filename = "c: // test. iso ";
If (get_file_size (filename, & size) =-1) // get the file size
{
Perror ("get file size ");
Exit (1 );
}
If (pos = (char *) malloc (sizeof (char) * (size + 1) = NULL)
{
Perror ("malloc buf ");
Exit (1 );
}
If (fp = fopen (filename, "rb") = NULL) // read the file
{
Perror ("fopen file ");
Exit (1 );
}
If (fread (pos, 1, size, fp) <0) // failed to read the file
{
Perror ("fread file ");
Exit (1 );
}
MYSQL * mysql = mysql_init (NULL); // mysql initialization
If (! Mysql)
Return;
If (! Mysql_real_connect (mysql, host, username, password, "test", port, NULL, 0) // link to the server
{
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 = pos;
// 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;
}

Running result:
BitsCN.com

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.