Introduction to SQLITE3 C Language interface API functions
Description: This document belongs to the author from the contact SQLite began to understand the use of API functions, from my translation, constantly updated.
/*2012-05-25*/IntSqlite3_open (Constchar* filename,/*Database file name, must be in UTF-8 format*/sqlite3** ppdb/*Output: SQLite database handle*/Description: This function opens the database specified by filename, and a database connection handle is returned by *ppdb (regardless of whether an error has occurred). The only exception is that SQLite cannot allocate memory space for SQLite objects, and NULL is returned. If the database is opened successfully (and/or created), the function returns SQLITE_OK (0). Otherwise, an error code is returned, and the cause of the error can be obtained by sqlite3_errmsg (). You should use Sqlite3_close () to close the database connection, regardless of whether the database was successfully opened. Example:int ret =0;char* filename ="./nbsg.db"; sqlite3* PDB =NULL; To_utf8 (filename); ret = Sqlite3_open (filename, &PDB);if (ret! =SQLITE_OK) ...IntSqlite3_close (sqlite3* PDB/**/ Finish ( Finalize) " precompiled statements ( Prepared statements) " "If there is an unfinished precompiled statement or binary handle when closed, the function returns SQLITE_BUSY (5if (PDB!= NULL) {sqlite3_close (PDB); PDB = NULL;}
/*2012-05-26*/IntSqlite3_errcode (sqlite3* PDB/*SQLite3 Database Handle*/Description: This function returns the error code that was generated when the Sqlite3_ API was last called. Example:int Errcode =Sqlite3_errcode (PDB);const char *sqlite3_ ErrMsg (sqlite3* pDB /* SQLite3 database handle */%s\n sqlite3_errmsg (PDB)); The girl does not cry (Qq:191035066) @2012- 05-26 11:31:54 @ http://
/*Sqlite3_exec (), 2012-05-28*/IntSqlite3_exec (sqlite3* PDB,/*Sqlite3 handle*/Constchar* SQL,/*The SQL statement that was executed*/Int (*callback) (void*,Intchar**,char**),/*Execute/Query callback function*/void* pvoid,/*The first argument passed to a callback function*/Char**errmsg/*Error output Information*/Description: This function is used to execute several SQL statements. This function wraps the previous version of the Sqlite3_prepare (), Sqlte3_step (), and sqlite3_finalize () functions so that the user can execute multiple SQL statements with simple code execution. The Sqlite3_exec () interface executes more than one";"A delimited SQL statement. If the callback function is not NULL, it will call the callback function for each row query result. If no callback function is specified, sqlite3_exec () simply ignores the query results. When an error occurs executing the SQL statement, execution breaks and all subsequent statements are ignored. If the errmsg parameter is not empty any error messages will be written into the memory space obtained by Sqlite3_malloc (), which is the memory that errmsg points to. To avoid a memory leak, the application should call Sqlite3_free () to free the memory space immediately after the error message is not needed. If the errmsg parameter is not NULL and no error occurs, ERRMSG is set to null. If the callback function returns nonzero, sqlite3_exec () immediately interrupts the query, and no subsequent SQL statements are executed, and the callback function is no longer called, and Sqlite3_exec () returns Sqlite_abort to the end of execution. Example: Sqlite3_exec (PDB , To_utf8 ("Delete from tablename where id=123;"), NULL, NULL, NULL); Sqlite3_exec (PDB, To_utf8 ( "create table if not EXISTS tablename (ID integer Primary Key,name text); " "insert into TableName (name ) VALUES (' Girls do not cry '); "if (Sqlite3_exec (PDB, To_utf8 ( " select * from TableName; SQLITE_OK {... sqlite3_free (pszerrmsg); pszerrmsg = NULL;}
See: Using Callback Functions in Sqlite3 (http://www.cnblogs.com/nbsofer/archive/2012/05/29/2523807.html)
/*2012-05-29, Sqlite3_prepare (), Sqlite3_step (), Sqlite3_finalize ()*/IntSqlite3_prepare (sqlite3* PDB,/*Successfully opened database handle*/Constchar* SQL,/*UTF8 encoded SQL Statements*/int Nbytes,/*The number of bytes in the parameter SQL, including '*/sqlite3_stmt** ppstmt,/*Output: Precompiled statement handle*/Constchar** Psztail/*Output: Pointing to unused portions of the SQL statement*/Note: To execute an SQL query, it must be compiled into bytecode before it can be used by other functions. If Nbytes is less than 0, the SQL statement takes the first‘/Endpoint. The maximum length of the read if it is non-negative. When Nbytes is greater than0 o'clock, the specified length is read, if‘/' Be read first, then‘/End. If the user knows that the SQL statement being passed in is‘/' End, then there is a better way to do this: set the value of the nbytes to the length of the string (containing‘/‘), which prevents SQLite from copying a copy of the string to improve the efficiency of the program. If Psztail is not NULL, *psztail points to the end of the first SQL statement that was passed in SQL. This function compiles only the first statement of SQL, so *The content that Psztail points to is not compiled. *ppstmt points to a pre-compiled statement that can be used by the Sqlie3_step () function. If an error occurs, *The value of pszstmt is null. The caller should use Sqlite3_finalize () to remove the precompiled statement. If the function succeeds, returns SQLITE_OK, otherwise an error code is returned.IntSqlite3_step (sqlite3_stmt* ppstmt/*A precompiled SQL statement*/NOTE: When a statement is precompiled by Sqlite3_prepare () or its associated function, Sqlite3_step () must be called one or more times to evaluate the precompiled statement. The detailed behavior of the function relies on the Sqlite3_prepare () (or its related functions) to produce a precompiled statement. The function returns one of the following results to identify its execution result: sqlite_busy: Busy. The database engine cannot lock data to complete its work. But you can try it multiple times. Sqlite_done: Complete. The SQL statement has been successfully executed. The currently precompiled statement should not be called again by Sqlite3_step () until Sqlite_reset () is called. Sqlite_row: Results were generated when querying. You can now use the relevant data Access function (column access functions) to get the data. Another call to Sqlite3_step () will get the next query result. Sqlite_error: An error has occurred. The relevant error message can be obtained by sqlite3_errmmsg () at this time. Sqlite3_step () cannot be called again. Sqlite_misuse: Incorrect use of the library. This function is not used properly. Other: The use of precompilation is explained in detail in a later function introduction. int sqlite3_finalize (sqlite3_stmt* pstmt/ * pre-compiled statement */); Description: This function is used to delete an example of a precompiled SQL statement: sqlite3_ Finalize (PSTMT); pstmt = NULL;
Go Introduction to SQLITE3 C Language interface API functions