SQLite Tutorial
|
|
|
|
| Articles Reviews Structured Query Language | |
| Written by Mike Chirico | |
| Monday, 27 November 2006 | |
|
Page 10 of 12
Defining SQLite User Functions There are two types of functions, aggregate functions and simple functions. Simple functions like sign(), which was created above, can be used in any expression. Aggregate functions like "avg()" can only be used in the select statement. Some functions like "min" and "max" can be defined as both. min() with 1 argument is an aggregate function, whereas min() with an unlimited number of arguments is a simple function. $ sqlite3 Note above that the min() function, with only one variable, is an aggregate function. Since it is only an aggregate function, it cannot be used after the where clause. An aggregate function can only be used after the select clause as follows: sqlite> select min(a) from junk If you add a second argument, you're calling the simple function. Note below that each row is compared to 2.3. Look closely; there is a subtle but important difference here. sqlite> select min(a,2.3) from junk Creating a User-defined Sign Function: msign void msignFunc(sqlite3_context *context, int argc, sqlite3_value **argv){ This function is initiated as follows: sqlite3_create_function(db, "msign", 1, SQLITE_UTF8, NULL, Note that "msign" is the name of the function in sqlite3. It is the name you would use in a select statement: "select msign(3);". The 1 is the number of arguments. The msign function here only takes one argument. SQLITE_UTF8 is for the text representation. Then, skipping over NULL, &msignFunc is the name of the C function. The last two values must be NULL for a simple function; again, a simple function can be used in any part of the select where clause. |
|
| Last Updated ( Sunday, 06 January 2008 ) | |
| < Prev | Next > |
|---|







