Recently, the company has arranged a task to create a tool to insert files to MySQL in Linux in windows. If it is just a simple plug-in, there should be tools available for download on the Internet, but I did not find it. I implemented one myself. I recorded some of the things used in the development process. If I encounter it again later, I can check it here. To communicate with MySQL, I chose to directly use MySQL's c API to conveniently and quickly operate MySQL and easily implement cross-platform operations. If ODBC is used, in * nix, You have to rewrite a set of code. To save a file, MySQL needs to set the corresponding field to blob or longblob type. If it is another type, such as text, it may lose data if it is saved. Insert blob in MySQL is implemented through the insert statement. It reads the binary stream of the file and escapes it into an identifiable ASCII code. This is done by the mysql_real_escape_string function, then, call mysql_real_query for insertion. Note that mysql_real_query must be called here, which is described in the MySQL manual as follows: You must use mysql_real_query () rather than mysql_query () for queries that contain binary data, because binary data may contain in the '/0' character. in addition, mysql_real_query () is faster than mysql_query () because it does not call strlen () on the query string. After talking about this, the following is an example of a function I wrote. Note that this function is written in pure C and is not associated with C ++: Int my_upload_blob (MySQL * dB, const char * filepath, const char * cmd_prev, int prev_len, const char * cmd_next, int next_len) { Unsigned long Len = 0; Unsigned long size = 0; Char * Data = NULL; Char * chunk = NULL; Char * query = NULL; Char * Pos = NULL;
File * FP; Fp = fopen (filepath, "rb "); If (null = FP) { Return err_open_file; } Data = malloc (1000*1024);/* 1 M */ Chunk = malloc (2x1000*1024 + 1);/* 2 M */ Query = malloc (1024*5000);/* 5 m */ Pos = query; Size = fread (data, 1, 1024*1000, FP ); Size = mysql_real_escape_string (dB, Chunk, Data, size );
/* Copy SQL to query */ Len = prev_len; Strncpy (query, performance_prev, Len ); Pos = query + Len; /* Copy binary file data to query */ * POS ++ = '/''; Strncpy (Pos, Chunk, size ); Pos + = size; * POS ++ = '/''; /* If next is not null, a comma is needed */ If (cmd_next) { * POS ++ = ','; } /* Copy SQL to query */ Len = next_len; Strncpy (Pos, cmd_next, Len ); Pos + = Len; /* Shocould call mysql_real_query */ If (0! = Mysql_real_query (dB, query, pos-query )) { Free (data ); Free (chunk ); Free (query ); Fclose (FP ); Return err_mysql_query; }
Free (data ); Free (chunk ); Free (query ); Fclose (FP ); Return 0; } Attached download function: Int my_download_blob (MySQL * dB, char * filepath, char * cmd, int field_pos) { Unsigned long * lengths; File * FP; Fp = fopen (filepath, "WB "); If (null = FP) { Return err_open_file; }
If (0! = Mysql_query (dB, CMD )) { Return err_mysql_query; }
Mysql_res * _ res = mysql_store_result (db ); Mysql_row _ ROW = mysql_fetch_row (_ res ); If (null = _ res) { /* Add your code here */ } Lengths = mysql_fetch_lengths (_ res ); Fwrite (row [field_pos], lengths [field_pos], 1, FP ); Fclose (FP ); Mysql_free_result (_ res ); Return 0; } This blog does not repost any articles. If you use the code in this blog, please reply with me. |