# Include <stdio. h>
# Include <sqlite3.h>
Static int callback (void * notused, int argc, char ** argv, char ** azcolname)
/** The second parameter of callback is the number of columns in the query result, and the third parameter is the string array containing the values of each column.
The fourth parameter is the column characters of each column. Callback can be null. */
{
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 );
}
Rc = sqlite3_exec (dB, argv [2], callback, 0, & zerrmsg );
/// Rc = sqlite3_exec (Open Database, SQL statement, call callback function, this void * is the first parameter of callback
Data transmission to callback, & zerrmsg );
/**
If sqlite_ OK is returned for this function, the execution is successful.
If sqlite_busy is returned, the database is locked or busy (in this case, you can use sqlite3_busy_handler () and
Sqlite3_busy_timeout ).
The callback function usually returns 0. If a non-0 value is returned, the query is aborted, and the SQL statement is skipped and sqlite3_exec () returns
Sqlite_abort.
When an error occurs during SQL statement execution (not when callback is executed), the error message is written to the memory obtained using malloc,
* Errmsg points to the memory. This memory should be released by callback. You can call sqlite3_free (). If
Errmsg = NULL indicates no error message.
What is the void pointer?
Void pointers are generally called "generic Pointers" or "generic Pointers ". The void pointer can be easily converted to a pointer of another data type.
For example, when allocating memory space for a pointer:
Int * P;
P = (int *) malloc (......);
The Return Value of the malloc function is of the void type. The Void * type is converted to the int * type by adding an int * with parentheses.
.
*/
If (RC! = Sqlite_ OK)
{
Fprintf (stderr, "SQL error: % s \ n", zerrmsg );
}
Sqlite3_close (db );
Return 0;
}
2005.8.1-use sqlite.txt in the program