Use sqlite.txt in the program

Source: Internet
Author: User
Tags sql error

# 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

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.