at night a small study of the next MySQL stored in the Read binary data function. The key step is the following three points: the most important point: the type of table that stores the binary data needs to be a blob type (divided by length tiny, media,Longwhen inserting binary data, you need to use the Mysql_real_escape_string function to convert the data to read binary data from the database, you need to take advantage of the Mysql_fetch_length function number segment length, which needs to be Mysql_fetch_ Row is called after the results can be obtained by a C write a small example of storage and reading fields, write a relatively rough, forgive me ha~First, one. Insert: Copy codeintDb_insert_object (void*Object, unsignedintobjsize) { intret; Char*escape_object =NULL; Charsql[1024x768]; unsignedintEscape_size =2* ObjSize +2; intSql_len; MYSQL*mysql=NULL; /*Connnect DB*/MySQL=Mysql_init (NULL); if(!mysql_real_connect (MySQL, SERVER, USER, PASSWD, DATABASE,0Null0) ){ GotoError1; } /*Convert binary string*/Escape_object= (Char*) malloc (escape_size); if(Escape_object = =NULL) { GotoError1; } escape_size= mysql_real_escape_string (MySQL, Escape_object, (Char*)Object, objsize); Sql_len= sprintf (SQL,"insert into task (object) values ('%s ')", Escape_object); RET=mysql_real_query (MySQL, SQL, Sql_len); if(ret) {GotoError1; } free (escape_object); Mysql_close (MySQL); return 0; error1:printf ("Error:%s\n", Mysql_error (MySQL)); if(MySQL) mysql_close (MySQL); if(escape_object) free (escape_object); return-1;} The copy code is then read: Copy codevoid*Db_fetch_object () {MYSQL*mysql=NULL; Mysql_res*res =NULL; Mysql_row ROW; unsignedLong*Row_len; Char*Object=NULL; Const Char*sql ="Select object from task Limit 1"; unsignedLongobjsize; intret; /*Connnect DB*/MySQL=Mysql_init (NULL); if(!mysql_real_connect (MySQL, SERVER, USER, PASSWD, DATABASE,0Null0) ){ GotoError2; } /*Get Object*/ret=mysql_real_query (MySQL, SQL, strlen (SQL)); if(ret) {GotoError2; } Res=mysql_store_result (MySQL); if(res = =NULL) { GotoError2; } /*Important*/Row=mysql_fetch_row (RES); Row_len= Mysql_fetch_lengths (res);/*get the object ' s length*/ if(Row_len = =NULL) { GotoError2; } objsize= row_len[0]; Object= (Char*) malloc (objsize); if(Object==NULL) { GotoError2; } memcpy (Object, row[0], objsize); Mysql_close (MySQL); Mysql_free_result (RES); return(void*)Object; error2:printf ("Error:%s\n", Mysql_error (MySQL)); if(RES) mysql_free_result (res); if(MySQL) mysql_close (MySQL); if(Object) Free (Object); returnNULL;}
There seems to be a direct point. The MySQL API can store binary data.
MySQL Stores binary data