# 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; } |