Sqlite3 C language interface API function Introduction

Source: Internet
Author: User

Note: This document describes the usage of API functions recognized by the author since his access to SQLite. This document is translated and updated by myself.

 /*  2012-05-25  */  Int  Sqlite3_open (  Const   Char * Filename, /*  Database file name, which must be in UTF-8 format  */  Sqlite3 ** Ppdb /*  Output: SQLite database handle  */ ); Note: This function opens the database specified by filename, and a database connection handle is * Ppdb returns (whether an error occurs or not ). the only exception is that SQLite cannot allocate memory space for the SQLite object. At this time, null is returned. if the database is successfully opened (and/or created), the function returns sqlite_ OK ( 0  ). Otherwise, an error code is returned. You can use sqlite3_errmsg () to obtain the cause of the Error. Whether or not the database is successfully opened, use sqlite3_close () to close the database connection. Example:  Int Ret = 0  ;  Char * Filename = "  ./Nbsg. DB  "  ; Sqlite3 * PDB =NULL; to_utf8 (filename); RET = Sqlite3_open (filename ,& PDB );  If (Ret! = Sqlite_ OK )...  Int  Sqlite3_close (sqlite3 * PDB /*  SQLite object handle opened by sqlite3_open or base-related functions  */  ); Description: This function is used to analyze the sqlite3 object. The returned sqlite_ OK indicates that the object is successfully analyzed, and all related resources are successfully recycled.ProgramMust be disabled before  "  Finalize) " All "  Precompiled Statement (Prepared statements)  " And disable all "  Bind a binary handle (BLOB handle)  " If there are still unfinished pre-compiled statements or binary handles when the function is disabled, the function returns sqlite_busy ( 5  ). Example:  If (PDB! = Null) {sqlite3_close (PDB); PDB = NULL ;} 
 /*  2012-05-26 */  Int  Sqlite3_errcode (sqlite3 * PDB /*  Sqlite3 database handle  */  ); Note: This function returns the error code generated when calling sqlite3 _ API the last time. Example:  Int Errcode = Sqlite3_errcode (PDB );  Const   Char * Sqlite3_errmsg (sqlite3 * PDB /*  Sqlite3 database handle */  ); Note: This function returns error messages related to PDB database pointers, written in English. the user does not have to consider the release of memory, which is managed internally by SQLite. It will also be overwritten when the function is called next time. example: printf (  "  % S \ n  "  , Sqlite3_errmsg (PDB); girls do not cry (QQ:  191035066 )@ 2012 - 05 - 26   11 : 31 : 54 @ Http: // Www.cnblogs.com/nbsofer     
 /*  Sqlite3_exec (), 2012-05-28  */  Int  Sqlite3_exec (sqlite3 * PDB, /*  Sqlite3 handle  */      Const   Char * SQL, /*  Executed SQL statement  */      Int (* Callback )(Void *, Int , Char **, Char **), /*  Execution/query callback function  */      Void * Pvoid, /*  The first parameter passed to the callback function  */      Char ** Errmsg /*  Error output  */ ); Description: This function is used to execute several SQL statements. this function encapsulates the previous versions of sqlite3_prepare (), sqlte3_step () and sqlite3_finalize () functions, so that you can execute simpleCodeExecute Multiple SQL statements. sqlite3_exec () interface to execute multiple  "  ;  "  Separate SQL statements. if the callback function is not null, the callback function is called for the query results of each row. if no callback function is specified, sqlite3_exec () simply ignores the query results. when an error occurs during SQL statement execution, the execution is interrupted and all subsequent statements are ignored. if the errmsg parameter is not empty, any error information will be written into the memory space obtained by sqlite3_malloc (), that is, the memory pointed by errmsg. to avoid Memory leakage, the application should call sqlite3_free () to release the memory immediately after the error message is not required. if the errmsg parameter is not null and no error occurs, errmsg is set to null. if the callback function returns a non-zero value, sqlite3_exec () immediately interrupts the query and does not execute subsequent SQL statements or call the callback function. sqlite3_exec () returns sqlite_abort to terminate the execution. example: sqlite3_exec (PDB, to_utf8 (  " Delete from tablename where id = 123;  "  ), Null); sqlite3_exec (PDB, to_utf8 (  "  Create Table if not exists tablename (ID integer primary key, Name text );  "  ), Null); sqlite3_exec (PDB, to_utf8 (  "  Insert into tablename (name) values ('Girls do not cry ');  "  ), Null );  If (Sqlite3_exec (PDB, to_utf8 ( " Select * From tablename;  " ), Sqlite_callback, null, & pszerrmsg )! = Sqlite_ OK) {... sqlite3_free (pszerrmsg); pszerrmsg = NULL ;}
See: Using callback functions (http://www.cnblogs.com/nbsofer/archive/2012/05/29/2523807.html) in sqlite3)
 /*  2012-05-29, sqlite3_prepare (), sqlite3_step (), sqlite3_finalize ()  */  Int  Sqlite3_prepare (sqlite3 * PDB, /*  Successfully opened database handle */      Const   Char * SQL, /*  Utf8 encoded SQL statement  */      Int Nbytes, /*  The number of bytes of the parameter SQL, including '\ 0'  */  Sqlite3_stmt ** Ppstmt, /*  Output: Pre-compiled statement handle  */      Const  Char ** Psztail /*  Output: point to the unused part of the SQL statement.  */  ); Note: to execute an SQL query, it must be first compiled into a bytecode before it can be used by other functions. If nbytes is smaller than zero, the SQL statement uses the first  '  \ 0  ' End. If it is not negative, it is the maximum read length. When nbytes is greater 0 Read the specified length. If '  \ 0  ' Read first '  \ 0 ' End. If the user knows that the passed SQL statement is '  \ 0  ' So there is a better way: set the value of nbytes to the length of the string (including '  \ 0  '  In this way, SQLite can avoid copying a copy of the string to improve program efficiency. If psztail is not null * Psztail points to the end of the first SQL statement passed in SQL. This function only compiles the first SQL statement, so * The content pointed to by psztail 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 delete the prepared statement. If the function is successful, sqlite_ OK is returned; otherwise, an error code is returned.  Int Sqlite3_step (sqlite3_stmt * Ppstmt /*  A pre-compiled SQL statement  */  ); Note: after a statement is precompiled by sqlite3_prepare () or its related functions, sqlite3_step () must be called once or multiple times to evaluate the prepared statement. the detailed behavior of this function depends on the precompiled statement generated by sqlite3_prepare () (or its related function. the function returns the following result to identify the execution result: sqlite_busy: Busy. the database engine cannot lock data to complete its work. however, you can try it multiple times. sqlite_done: complete. the SQL statement has been successfully executed. before calling sqlite_reset (), the current pre-compiled statement should not be called again by sqlite3_step. sqlite_row: results are generated during query. in this case  "  Column access functions)  " To obtain data. the next query result is returned when you call sqlite3_step. sqlite_error: An error occurred. in this case, you can use sqlite3_errmmsg () to obtain relevant error information. sqlite3_step () cannot be called again. sqlite_misuse: Incorrect library usage. this function is improperly used. others: the usage of pre-compilation will be described in detail in future function introduction.  Int  Sqlite3_finalize (sqlite3_stmt * Pstmt /*  Prepared statement  */  ); Note: this function is used to delete a pre-compiled SQL statement example: sqlite3_finalize (pstmt); pstmt = NULL;

<Pre_for_use>

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.