Using callback functions in SQLite3

Source: Internet
Author: User

The callback function is formatted as follows :

int Sqlite_callback (

void* PV, /* passed by The fourth parameter of sqlite3_exec () */

int argc,/ * number of columns in table * /

Char* * argv, /* pointer to the query result array , can be sqlite3_column_text () Get */

Char* */col * * an array of pointers to table header names can be used by Sqlite3_column_name () get */

);


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 ( data-in-process ) |

|-----------------------------------|

|  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 data , not in 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 : using atoi () to convert the result to an integer ( Integer), in the case of binary data , you can directly enforce type conversions , 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]);

}


Using callback functions in SQLite3

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.