Examples of SQLite User-Defined Functions

Source: Internet
Author: User
Examples of SQLite User-Defined Functions
 
Author: tamsyn Source: dongqiang@gmail.com time: 2007-3-20 "Font: large medium and small" 〖 scrolling 〗
 
 

# Include <stdio. h>
# Include <string. h>
# Include <stdlib. h>
# Include <time. h>
# Include <malloc. h>
# Include <assert. h>
# Include <sqlite3.h>

# Define select_query "select host_id, geturl (DESC) from host_table;

Void host2url_func (sqlite3_context * CTX, int argc, sqlite3_value ** argv)
{
If (argc! = 1) return;
Char * httpurl = NULL;
Switch (sqlite3_value_type (argv [0])
{
Case sqlite_null:
{
Sqlite3_result_text (CTX, "null", 4, sqlite_static );
Break;
}
Case sqlite_text:
{
Httpurl = (char *) malloc (strlen (sqlite3_value_text (argv [0]) + 8 );
If (httpurl = 0) return;
Sprintf (httpurl, "http: // % s", sqlite3_value_text (argv [0]);
Sqlite3_result_text (CTX, httpurl, strlen (httpurl), sqlite_transient );
Free (httpurl );
Httpurl = NULL;
Break;
}
Default:
Sqlite3_result_text (CTX, "about: blank", 11, sqlite_static );
}
}

Static int callback (void * notused, int argc, char ** argv, char ** azcolname)
{
Int I;
For (I = 0; I <argc; I ++ ){
Printf ("% s = % s/n", azcolname [I], argv [I]? Argv [I]: "null ");
}
Printf ("/N ");
Return 0;
}

Int main (INT argc, char ** argv)
{
Sqlite3 * dB;
Char * zerrmsg = 0;
Int RC;

If (argc! = 3)
{
Fprintf (stderr, "Usage: % s Database SQL-STATEMENT/N", argv [0]);
Exit (1 );
}
Rc = sqlite3_open (argv [1], & dB );
If (RC ){
Fprintf (stderr, "can't open database: % s/n", sqlite3_errmsg (db ));
Sqlite3_close (db );
Exit (1 );
}

Sqlite3_create_function (dB, "geturl", 1, sqlite_any, null, host2url_func, null, null );
Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg );

If (RC! = Sqlite_ OK ){
Fprintf (stderr, "SQL error: % s/n", zerrmsg );
}

Sqlite3_close (db );
Return 0;

}

 

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.