SQLite learning Manual (C/C ++ interface Introduction)

Source: Internet
Author: User

I. Overview:

Five of the C/C ++ interfaces provided by SQLite are core interfaces. In this blog, we will mainly introduce their usage and the core SQLite objects they involve, such as database_connection and prepared_statement. Compared with APIs provided by other database engines, such as OCI and MySQL APIs, the APIS provided by SQLite are very easy to understand and master.

Ii. core objects and interfaces:

1. Core objects:
The two most important objects in SQLite are, Database_connection And Prepared_statement . The database_connection object is created and returned by the sqlite3_open () interface function. Program Before using any other SQLite interface function, you must call this function to obtain the database_connnection object. In other subsequent APIs calls, you must use this object as the input parameter to complete the corresponding work. As for prepare_statement, we can simply regard it as the compiled SQL statement. Therefore, all functions related to SQL statement execution also need this object as an input parameter to complete the specified SQL operation.

2. Core interfaces:
1). sqlite3_open
As mentioned above, this function is the entry function for operating the SQLite database. The database_connection object returned by this function is a handle parameter of many other SQLite APIs. Note: You can use this function to open existing database files or create new database files. For the database_connection object returned by this function, we can share the pointer of this object among multiple threads to complete any database-related operations. However, in the case of multithreading, we recommend that you create an independent database_connection object for each thread. This function also requires additional instructions. We do not need to create multiple database connection objects to access multiple databases, the attach Command provided by SQLite allows you to easily access multiple databases in one connection.

2). sqlite3_prepare
This function converts SQL text to a prepared_statement object and returns a pointer to this object after the function is executed. In fact, this function does not evaluate the parameter-specified SQL statement. It only initializes the SQL text to the State to be executed. Finally, we can use the sqlite3_prepare_v2 interface function in place of the new application to complete the same work.

3). sqlite3_step
Evaluate the prepared_statement object returned by the sqlite3_prepare function. After the function is executed, the internal pointer of the prepared_statement object points to the first row of the returned result set. If you want to further iterate the data rows, you need to call the function until all the data rows have been traversed. However, for DML statements such as insert, update, and delete, this function can be executed once.

4). sqlite3_column
This function is used to obtain the data of the specified column in the current row. However, in strict sense, this function does not exist in the SQLite interface function, A group of related interface functions are used to complete this function. Each function returns different types of data, such:
Sqlite3_column_blob
Sqlite3_column_bytes
Sqlite3_column_bytes16
Sqlite3_column_double
Sqlite3_column_int
Sqlite3_column_int64
Sqlite3_column_text
Sqlite3_column_text16
Sqlite3_column_type
Sqlite3_column_value
Sqlite3_column_count
The sqlite3_column_count function is used to obtain the field data in the current result set. The following is the pseudo data of each row in the result set iterated using the sqlite3_step and sqlite3_column functions. Code Note that the Sample Code simplifies the determination of the field type:

  1   int  fieldcount = sqlite3_column_count (...); 
2 while (sqlite3_step (...) <> EOF) {
3 for ( int I = 0 ; I 4 int V = sqlite3_column_int (..., i);
5 }< br> 6 }

5). sqlite3_finalize
This function is used to destroy the prepared statement object. Otherwise, memory leakage may occur.

6). sqlite3_close
This function is used to close the previously opened database_connection object. All prepared_statements objects related to this object must be destroyed before this.

3. Parameter binding:

Like most relational databases, SQLite SQL text also supports variable binding to reduce the number of times SQL statements are dynamically parsed, thus improving the efficiency of data query and data operations. To complete this operation, we need to use the APIS, sqlite3_reset, and sqlite3_bind interfaces provided by SQLite. See the following example:

 1       Void Test_parameter_binding (){
2 // 1. Insert multiple data entries without parameter binding.
3 Char Strsql [ 128 ];
4 For ( Int I = 0 ; I <max_rows; ++ I ){
5 Sprintf (strsql, " Insert into testtable values (% d) " , I );
6 Sqlite3_prepare_v2 (..., strsql );
7 Sqlite3_step (prepared_stmt );
8 Sqlite3_finalize (prepared_stmt );
9 }
10 // 2. Insert multiple data entries when the parameters are bound.
11 String Strsqlwithparameter = " Insert into testtable values (?) " ;
12 Sqlite3_prepare_v2 (..., strsql );
13 For ( Int I = 0 ; I <max_rows; ++ I ){
14 Sqlite3_bind (..., I );
15 Sqlite3_step (prepared_stmt );
16 Sqlite3_reset (prepared_stmt );
17 }
18 Sqlite3_finalize (prepared_stmt );
19 }

The first thing to note here is that the SQL statement "insert into testtable values (?) "Question mark in(?)Represents the placeholder of parameter variables. This rule is consistent in many relational databases, so it is more convenient for database transplantation.
From the sample code above, we can see that the execution efficiency of parameter binding is higher than that of generating different SQL statements each time, that is, 2) the efficiency is significantly better than 1 ), the following is a comparison of the two methods:
1). On the program surface, the former executes more tasks in the for loop, such as string filling, SQL statement prepare, and prepared_statement object release.
2) according to the official SQLite documentation, the execution efficiency of sqlite3_prepare_v2 is usually lower than that of sqlite3_step.
3) when the inserted data volume is large, the efficiency improvement brought by the latter is considerable.

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.