Some time ago in doing an embedded project, using the SQLite database, now SQLite database to access the picture problem to share with you, for binary data we naturally can not be directly stored in the SQLite we can convert it and then store.
Let's get down to it.
Before the main clause operation we have to open the database, SQLite to provide users with a rich API, enough to enable us to control it, using the Sqlite3_open () function, open the database we want to operate, the next is the operation of our picture files, The way I am here is to convert the picture file into a character stream and store it.
Let me explain the problem with a simple example ...
#include <stdio.h> #include <sqlite3.h> #include <stdlib.h> static sqlite3 *db=null;
Static sqlite3_stmt *stmt=null;
FILE *FP;
Long filesize=0;
Char *fflie;
int main (int argc, char *argv[]) {int rc,i,j;
rc = Sqlite3_open ("dishes.db", &db);
rc = Sqlite3_prepare (db, "update dishes_table set Dish_image=?where dish_name= ' X ';",-1, &stmt, 0);
Fp=fopen ("X.jpg", "RB");
if (FP!= NULL) {fseek (FP, 0, Seek_end);
FileSize = Ftell (FP);
Fseek (FP, 0, Seek_set);
Ffile = (char *) malloc (filesize + 1);
size_t sz = fread (ffile, sizeof (char), filesize+1, FP);
Fclose (FP);
} sqlite3_bind_blob (stmt, 1, Ffile, FileSize, NULL);
Rc=sqlite3_step (stmt);
Free (ffile);
Sqlite3_finalize (stmt);
Sqlite3_close (DB);
return 0; }