MySql2.cpp: Definestheentrypointfortheconsoleapplication.
MySql2.cpp: Defines the entry point for the console application.
# Include "stdafx. h"
// Is the companion Article of the previous Article
// The Code comes from the network. I learned how to sort it out and passed the test. The following parameters
// Set it to your own
// You can test the Data Type of the www, table www, and file fields in the DBMS center by using LONGTEXT.
// 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
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;
# Include
# Include
# Include
# Include
# Include
# Include
# Include
# Define host "localhost" // mysql server
# Define username "root"
# Define password "674800"
# Define database "www"
Int port = 3306;
# Pragma comment (lib, "libmysql. lib ")
// Obtain the file size (in bytes)
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;
}
Int main (int argc, char * argv [])
{
Char * filename = NULL;
Off_t size;
MYSQL * conn = NULL;
MYSQL_RES * res_set = NULL;
MYSQL_ROW row;
MYSQL_FIELD * field = NULL;
Int I, flag;
Char * SQL; // SQL statement
FILE * fp;
Char * buf;
Int n = 256;
Char * end;
Unsigned long * length;
/* If (argc! = 2)
{
Printf ("Usage: % s srcfile \ n", argv [0]);
Exit (1 );
}
*/
Filename = "c: \ test. iso ";
If (get_file_size (filename, & size) =-1) // get the file size
{
Perror ("get file size ");
Exit (1 );
}
If (buf = (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 (n = fread (buf, 1, size, fp) <0) // failed to read the file
{
Perror ("fread file ");
Exit (1 );
}
SQL = (char *) malloc (sizeof (char) * n * 2 + 256); // 2n + 1 + strlen (other SQL)
If (SQL = NULL)
{
Perror ("malloc SQL ");
Exit (1 );
}
Conn = mysql_init (NULL); // produce a mysql object
If (conn = NULL)
{
Printf ("init mysql, % s \ n", mysql_error (conn ));
Exit (1 );
}
If (mysql_real_connect (conn, host, username, password, database, port, NULL, 0) = NULL) // connect to the server
{
Printf ("connect mysql, % s \ n", mysql_error (conn ));
Exit (1 );
}
Strcpy (SQL, "insert into www (id, name, file) values (NULL, 'Peter ',");
End = SQL;
End + = strlen (SQL); // point SQL tail
// Convert NUL (ASCII 0), '\ n',' \ R', '\ '', ''','" ', and Control-Z and so on
* End ++ = '\'';
End + = mysql_real_escape_string (conn, end, buf, n );
* End ++ = '\'';
* End ++ = ')';
Flag = mysql_real_query (conn, SQL, (unsigned int) (end-SQL ));
If (flag! = 0)
{
Printf ("insert failed, % s \ n", mysql_error (conn ));
Exit (1 );
}
If (mysql_real_query (conn, "SELECT file FROM www where id = 1", 31 ))! = 0)
{
Printf ("insert failed, % s \ n", mysql_error (conn ));
Exit (1 );
}
Res_set = mysql_store_result (conn );
Fclose (fp );
Fp = NULL;
Fp = fopen ("c: \ 123.iso"," wb ");
While (row = mysql_fetch_row (res_set ))! = NULL)
{
Length = mysql_fetch_lengths (res_set );
For (I = 0; I {
Fwrite (row [0], 1, length [0], fp );
// Printf ("% s \ n", row [0]);
}
}
Fclose (fp );
Mysql_close (conn );
Free (SQL );
Free (buf );
SQL = NULL;
Return 0;
}
Running result: