Sqlite-api-sqlite3_exec ()

Source: Internet
Author: User
Tags sqlite


Sqlite3_exec usage
Prototype:
int sqlite3_exec (  sqlite3* ppdb,/                             * An Open database */  const char *sql,/                           * SQL to IS evaluated */  INT (*callback) (void*,int,char**,char**),/  * Callback function */  void *,/                                    * 1st argument to Callback */  char **errmsg /                              * Error msg written here */);
Execute SQL statement
Get the result set, which can be output by a callback function
Has a callback function

This is the function that executes an SQL statement.
The 1th argument is no longer said to be the pointer to the previous open function. That's the key data structure.
The 2nd parameter, Constchar*sql, is an SQL statement that ends with a.
The 3rd parameter, Sqlite3_callback, is the callback, and when the statement executes, SQLITE3 will call the function you provided.
The 4th parameter, void*, is the pointer you provide, you can pass any pointer parameter here, and this parameter will eventually be passed to the callback function, if you do not need to pass the pointer to the callback function, you can fill null. Let's look at the write of the callback function again.
method, and the use of this parameter.
The 5th parameter, char** errmsg, is an error message. Note is a pointer to the pointer. There are many fixed error messages in the Sqlite3. After executing the sqlite3_exec, the pointer can be consulted when execution fails (direct cout<<errmsg gets a string of information that tells you where the error is.) The SQLITE3_EXEC function passes the pointer to the pointer you passed in, pointing to the error message with the pointer you provided, so that the sqlite3_exec function can get a specific error from the char* outside.

Description: Typically, both the sqlite3_callback and the void* behind it can be filled with null. Filling in null means you don't need a callback. For example, if you do an insert operation and do a delete operation, there is no need to use callbacks. And when you make a SELECT, you use callbacks, because sqlite3 the data to tell you what data was detected by a callback.

callback function format

int Sqlite_callback (

void* PV,/* passed by the fourth parameter of Sqlite3_exec () */
int argc,/* Number of columns in table */
char** argv,/* array of pointers to query results, can be obtained by sqlite3_column_text () *//column contents
char** Col/* An array of pointers to table header names, which can be obtained by sqlite3_column_name () *//Column Name
);
Parameter format:
callback function passed to Sqlite3_exec to display the query results
Call the callback function once for each query result
Parameters:
PV: Initialization parameters passed by sqlite3_exec
ARGC: Number of columns of the table header
Col: Name array pointer to table header
ARGV: Data array pointer to table header
return value:
1: Interrupt Lookup
0: Continue to enumerate the queried data
Sample table:
+-----------------------------------+
|  ID |  Pic | Data (16 binary data) |
|-----------------------------------|
|  1 |      a.jpg | 00 00 00 ... |
|-----------------------------------|
|  2 |     b.jpg | xx xx xx |
+-----------------------------------+
For the first row of data:
argc=3 [0] ... [2]
argv[0]= "1", argv[1]= "a.jpg", argv[2]= "00 00 00 ..." (actual 16 binary data, not the string form shown here)
col[0]= "id", col[1]= "pic", col[2]= "data"
Description
The callback function of Sqlite3_exec () must follow this format, with the name of the formal parameter arbitrarily.
If the data type of a column is not char*, you can perform related transformations on the result, such as converting the result to an integer (integer) with atoi () and, if it is binary data, you can directly enforce the type conversion, such as: (void*) argv[i].
The callback function has two types of return values.
1. Return 0: Sqlite3_exec () will continue to execute the query.
2. Return non 0: sqlite3_exec () will immediately break the query, and Sqlite3_exec () will return Sqlite_abort.
Example:
int i;
for (i=0; i<argc; i++)
{
printf ("%s\t%s\n\n", Col[i], argv[i]);
}

return 0;

Sqlite-api-sqlite3_exec ()

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.