You can test the database test, table bintest, and data field data type by using longblob in the middle line of the DBMS.
// Test file c: \ test. ISO. You can find any file and change it. Program , Changed to 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
CopyCode The Code is as follows: // 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 <winsock2.h>
# Include <mysql. h>
# Include <string. h>
# Include <stdio. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# 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: