In actual programming and development, we often need to process the storage of large-capacity binary data, such as slices or music. For these binary data (BLOB fields), we cannot simply insert or query the data as if we were dealing with common text. Therefore, SQLite provides a set of functions to process this blob field type. The followingCodeDemonstrate how to use these API functions.
First, we need to create a database:
Sqlite3_exec (dB, "create table list (fliename varchar (128) Unique, fzip BLOB);", 0, 0, & zerrmsg); // It is a binary file, so we need to use the insert statement first? Replace sqlite3_prepare (dB, "insert into list values ('mmmm.rar ',?); ",-1, & stat, 0); file * FP; long filesize = 0; char * ffile; FP = fopen (" mmmm.rar "," rb "); if (FP! = NULL) {// calculate the file size fseek (FP, 0, seek_end); filesize = ftell (FP); fseek (FP, 0, seek_set ); // read the file ffile = new char [filesize + 1]; size_t SZ = fread (ffile, sizeof (char), filesize + 1, FP); fclose (FP );} // bind the file data to the insert statement and replace "?"" Some sqlite3_bind_blob (stat, 1, ffile, filesize, null); // execute the bound SQL statement sqlite3_step (STAT );
At this time, the database already has a piece of data containing the Blob field. Next we will read this data:
// Select the data sqlite3_prepare (dB, "select * From list;",-1, & stat, 0); sqlite3_step (STAT ); // obtain the Blob field const void * test = sqlite3_column_blob (stat, 1) in the record. // obtain the Data Length int size = sqlite3_column_bytes (stat, 1) in the field ); // copy this field sprintf (buffer2, "% s", test );
At this time, buffer2 can be written to the file, so that BLOB data processing is complete.