Sqlite--c Language Interface Specification for iOS development (iv)--result Values from A Query

Source: Internet
Author: User
Tags sqlite database

The database in the previous blog in the "SQLite C Language Interface Specification (iii)--BINDING values to Prepared statements" used to remove the result value from the query results. Today's blog on the detailed introduction of sqlite3_column_* () method. In the SQLite database C language interface, it takes different interface functions to remove different types of values from the query results.

I. sqlite3_column_* () Introduction

1. Is the method contained in sqlite3_column_* () , it is easy to see that taking different types of values requires different interface functions. The types that can be removed are blobs, bytes, BYTES16, double, int, int64, text, Text16, and so on. The first parameter of the interface is our precompiled SQL statement (sqlite3_stmt object), and the second parameter is the number of rows to be fetched (from left to right, starting at 0). The information returned by these interfaces above is the value of a column in the current query row. In all cases, the first parameter is exactly the pointer to the precompiled statement (the sqlite3_stmt *returned by the sqlite3_prepare_v2 () function). The second parameter is the column index that should return information in rows (the leftmost column index of the result set 0). The number of columns in the result set can be obtained using sqlite3_column_count () .

    

If the SQL statement does not currently point to a valid row or column index that is out of range, then the result set is undefined. The above methods are called only in the case of calling the sqlite3_step () function and returning Sqlite_row , not in Sqlite3_reset () and sqlite3_ Finalize () invokes the above method after execution. If you do this, the result set will be indeterminate.

2. sqlite3_column_count () Use the following method, whose parameter is a pointer to the precompiled statement of Sqlite3_stms * , the return value is the number of columns of the current result set.

    // get all rows    for query results int columnCount = sqlite3_column_count (statement);        NSLog (@ "columnCount =%d", ColumnCount);  ColumnCount = 4

  

3. Sqlite3_column_type () This function returns the type code of the data on the corresponding column. The results returned are Sqlite_integer, Sqlite_float, Sqlite_text, Sqlite_blob , or sqlite_null one of the cases. The macros that correspond to the interfaces in the API are defined as follows.

    Sqlite3_column_type The call to () must be placed in the sqlite3_step() function (and the result is returned), otherwise null will be returned. Use the following methods:

1         int 1 ); 2         3         NSLog (@ "ColumnType =%d", ColumnType);  // ColumnType = 3 (sqlite_text)

4. If the type of the query result is a BLOB or UTF-8 string type, you can use the sqlite3_column_bytes() method to get the byte length of the data. If the result is a string of UTF-16, thesqlite3_column_bytes() method will automatically convert the string to UTF-8 string type, and then return the number of bytes in the string. SQLITE3_COLUMN_BYTES16 () usage is to get the number of bytes that the UTF-16 string value occupies, using the same usage as sqlite3_column_bytes8(). These two methods return not the number of characters in the string, but the number of bytes that the string occupies, of course, the number of bytes in the C language does not include the end of the string "\".

The specific usage of this function is as follows:

1         int 2 ); 2         3         NSLog (@ "%@ bytes =%d", firstletterstring, currentvaluebytes);

5. sqlite3_column_value() returns an unprotected Sqlite3_value object. In a multithreaded environment, an unprotected sqlite3_value object is only Sqlite3_bind_value() and sqlite3_result_value() The interface is safe to use.

Ii. Examples of Use

In the previous blog, the method of query traversal is expanded, the following method is expanded:

1 //Querying the database2- (void) Queryuserinfowith: (sqlite3 *) database withstatement: (SQLITE3_STMT *) Statement {3     4     //get all rows for query results5     intColumnCount =Sqlite3_column_count (statement);6     7NSLog (@"ColumnCount =%d", columnCount);8     9 Ten      One      while(Sqlite3_step (statement) = =Sqlite_row) { A          -          -         intRowNum = Sqlite3_column_int (statement,0); the          -         Char*rowdataone = (Char*) Sqlite3_column_text (statement,1); -          -         Char*rowdatatow = (Char*) Sqlite3_column_text (statement,2); +          -NSString *namestring =[NSString Stringwithutf8string:rowdataone]; +          ANSString *firstletterstring =[NSString Stringwithutf8string:rowdatatow]; at          -          -NSLog (@"Brandid =%d, Name =%@, Firstletter =%@", RowNum, namestring, firstletterstring); -          -          -          in         intColumnType = Sqlite3_column_type (statement,1); -          toNSLog (@"\"%@\"Type code =%d", namestring, ColumnType);//ColumnType = 3 (sqlite_text) +          -  the          *         intCurrentvaluebytes = sqlite3_column_bytes (statement,1); $         Panax NotoginsengNSLog (@"\"%@\"the number of bytes is =%d \ n", namestring, currentvaluebytes); -          the     } +      A sqlite3_finalize (statement); the      +}

Call the above method, the specific input results are as follows:

    

Today's content first here, the next blog back to a complete example, the SQL additions and deletions to the method of encapsulation, the database operation. The database used in the next blog can not be placed in the bundle, you need to copy it into the sandbox , and then to increase the deletion of the search. For details, see the next blog post (update later).

Database resources used in this blog GitHub share address:https://github.com/lizelu/SQLiteResource

Sqlite--c Language Interface Specification for iOS development (iv)--result Values from A Query

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.