SQLite Study Notes (9)

Source: Internet
Author: User

Expand C API

The basic method for implementing functions, aggregation, and sorting is to use the callback function, register it in the program, and then use it in SQL.

Functions and aggregate use the same registration function and similar callback function. The lifecycle of user-defined aggregation, functions, and sorting rules is very short. It is based on connection registration and is not stored in the database. You need to ensure that the application contains custom extensions and is registered in the connection.

Extensions must be registered on each connection that uses them.

Registration function:

You can use sqlite3_create_function () to register and aggregate data in the connection. (UTF-8 and UTF-16 Version)

Int sqlite3_create_function (

SQLite * CNX,/* connection handle */

Const char * zfunctionname,/* Name of function aggregation used in SQL statements */

Int NARG,/* parameter format.-1 indicates the number of parameters is unstable */

Int etextrep,/* encoding format */

Void * puserdata,/* application data that passes callback functions */

Void (* xfunc) (sqlite3_context *, Int, sqlite3_value *),

Void (* xstep) (sqlite3_context *, Int, sqlite3_value *),

Void (* xfinal) (sqlite3_context *, Int, sqlite3_value *)

);

Int sqlite3_create_function16 (

SQLite * CNX,/* connection handle */

Const char * zfunctionname,/* Name of function aggregation used in SQL statements */

Int NARG,/* parameter format.-1 indicates the number of parameters is unstable */

Int etextrep,/* encoding format */

Void * puserdata,/* application data that passes callback functions */

Void (* xfunc) (sqlite3_context *, Int, sqlite3_value *),

Void (* xstep) (sqlite3_context *, Int, sqlite3_value *),

Void (* xfinal) (sqlite3_context *, Int, sqlite3_value *)

);

Step Functions:

Like the callback function of a custom function, the statement is as follows:

Void FN (

Sqlite3_context * CTX,/* the aggregated Context Environment, accepting the application data parameters provided by sqlite3_create_function */

Int nargs,/* contains the number of parameters passed to the function */

Sqlite3_value ** values

);

You can use sqlite3_user_data () to obtain user data: void * sqlite3_user_data (sqlite3_context *);

Aggregate the status of each instance through sqlite3_aggregate_context (): void * sqlite3_aggregate_context (sqlite3_context *, int nbytes );

Return Value:

The parameter value is an array of sqlite3_value struct and a handle to the actual parameter value of SQLite. The actual data of these values can be obtained through sqlite3_value_xxx:

Xxx sqlite3_value_xxx (sqlite3_value * value );

Pay attention to the way in which the scalar value and array value are obtained.

Function return values in five forms.

Function: page221.

Returned value: page222;

Array and memory cleaner, error handling, returned input values, aggregation, and registration functions.

Pay attention to the definition of sorting rules.

Many of the extended rules are basically the same as those of the original core APIs.

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.