IPhone programming: How to Use APIs to operate BLOB fields in SQLite

Source: Internet
Author: User

IPhoneProgramming:SQLiteHow to Use API operations?BLOBType fields are the content to be introduced in this article. In actual programming and development, we often need to deal with storage of large-capacity binary data, such as slices or music. For these binary dataBlobFields ).SQLiteA set of functions are provided to process suchBLOBField type.

The following code demonstrates how to use these API functions.

First, we need to create a database:

 
 
  1. Sqlite3_exec (db, "create table list (fliename varchar (128) UNIQUE, fzip blob);", 0, 0, & zErrMsg );
  2.  
  3. /Since mmmm.rar is a binary file, do you need to use the insert statement first? No.
  4. Sqlite3_prepare (db, "insert into list values ('mmmm.rar ',?); ",-1, & stat, 0 );
  5. FILE * fp;
  6. Long filesize = 0;
  7. Char * ffile;
  8. Fp = fopen ("mmmm.rar", "rb ");
  9. If (fp! = NULL)
  10. {
  11. // Calculate the file size
  12. Fseek (fp, 0, SEEK_END );
  13. Filesize = ftell (fp );
  14. Fseek (fp, 0, SEEK_SET );
  15. // Read the file
  16. Ffile = new char [filesize + 1];
  17. Size_t sz = fread (ffile, sizeof (char), filesize + 1, fp );
  18. Fclose (fp );
  19. }
  20. // Bind the file data to the insert statement and replace "?" Part
  21. Sqlite3_bind_blob (stat, 1, ffile, filesize, NULL );
  22. // Execute the bound SQL statement
  23. Sqlite3_step (stat );

At this time, the database already has a piece of data containing the BLOB field. Next we will read this data:

 
 
  1. // Select the data.
  2. Sqlite3_prepare (db, "select * from list;",-1, & stat, 0 );
  3. Sqlite3_step (stat );
  4. // Obtain the BLOB field in the record
  5. Const void * test = sqlite3_column_blob (stat, 1 );
  6. // Obtain the data length in the field
  7. Int size = sqlite3_column_bytes (stat, 1 );
  8. // Copy this field
  9. Sprintf (buffer2, "% s", test );

At this time, buffer2 can be written to the file, so that BLOB data processing is complete.

Summary:IPhoneProgramming:SQLiteHow to Use API operations?BLOBThe content of the type field has been introduced. I hope this article will help you!

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.