# Include "stdafx. H"
// Is the companion Article of the previous Article
// Code From the network, I learned and sorted it out. The test passed. 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. 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
Copy code 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 <winsock2.h>
# Include <stdio. h>
# Include <mysql. h>
# Include <stdlib. h>
# Include <sys/types. h>
# Include <sys/STAT. h>
# Include <fcntl. h>
# 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 <mysql_num_fields (res_set); 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: