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:
- Sqlite3_exec (db, "create table list (fliename varchar (128) UNIQUE, fzip blob);", 0, 0, & zErrMsg );
-
- /Since mmmm.rar is a binary file, do you need to use the insert statement first? No.
- 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 "?" Part
- 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 in the record
- Const void * test = sqlite3_column_blob (stat, 1 );
- // Obtain the data length in the field
- Int size = sqlite3_column_bytes (stat, 1 );
- // Copy this field
- 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!