I recently started mysql. I want to use C to store binary files (64 k, large storage path) in the database over the past two days. after my unremitting search, I found that there is only one version on NET (php is used as an example), and it won't work in C. I first fainted, and then I searched again, or the php version, I vomited blood again (and then vomited again several times )! Finally, add the following code in mysq C and C ++.
I recently started mysql. I want to use C to store binary files (<64 k, large storage path) in the database over the past two days. after my unremitting search, I found that there is only one version on NET (php is used as an example), and it won't work in C. I first fainted, and then I searched again, or the php version, I vomited blood again (and then vomited again several times )! Finally, I found the direction in the mysql Manual. it was mysql_real_escape_string that gave me the light. okay, no worries. Otherwise, a water bottle will be smashed! In order not to let other beginners vomit blood, I will share my code. I hope you will correct me and welcome eggs and tomatoes.
CODE: [Copy to clipboard] # include
# Include
# Include
# Include
# Include
# Include
# Define host "localhost" // mysql server
# Define username "root"
# Define password "cipher"
# Define database "www"
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;
Off_t * size;
MYSQL * conn;
MYSQL_RES * res_set;
MYSQL_ROW row;
MYSQL_FIELD * field;
Int I, flag;
Char * SQL;
FILE * fp;
Char * buf;
Int n = 0;
Char * end;
Unsigned long * length;
If (argc! = 2 ){
Printf ("Usage: % s srcfile \ n", argv [0]);
Exit (1 );
}
Filename = argv [1];
If (get_file_size (filename, size) =-1 ){
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 ){
Perror ("fopen file ");
Exit (1 );
}
If (n = fread (buf, 1, * size, fp) <0) {// n = * size
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 );
If (conn = NULL ){
Printf ("init mysql, % s \ n", mysql_error (conn ));
Exit (1 );
}
If (mysql_real_connect (conn, host, username, password, database, 0, NULL, 0) = NULL ){
Printf ("connect mysql, % s \ n", mysql_error (conn ));
Exit (1 );
}
Strcpy (SQL, "insert into www (id, name, file) values (5, '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 = 5", 31 ))! = 0 ){
Printf ("insert failed, % s \ n", mysql_error (conn ));
Exit (1 );
}
Res_set = mysql_store_result (conn );
Fclose (fp );
Fp = NULL;
Fp = fopen ("foo. bk", "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 );
}
}
Fclose (fp );
Mysql_close (conn );
Free (SQL );
SQL = NULL;
Return 0;
}
// Gcc-o mysql_binary mysql_binary.c-lmysqlclient
// Usage:./mysql_binary filenameMakefile:
CODE: [Copy to clipboard] CXX = gcc
LIBS =-lmysqlclient
PRODUCT = mysql_binary
. LIBPATTERNS: lib %. so lib %.
Vpath %.
Vpath %. c src
Vpath %. h include
OBJS = mysql_binary.o
$ (PRODUCT): $ (OBJS)
$ (CXX) $ (OBJS)-o $ @ $ (LIBS)
. C. o:
. PHONY: clean
Clean:
-Rm-f $ (OBJS) $ (PRODUCT) good, wipe the sweat!
Dizzy so many emojis, get it done.
The generated foo. bk file is a copy of the source file.