How to access picture files in the SQLite database

Source: Internet
Author: User
Tags sqlite sqlite database stmt

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; }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.