How to Use APIs to operate BLOB fields in SQLite?

Source: Internet
Author: User

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.

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.