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 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
Copy codeThe 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: