Basics of SQLite (1): sqlite3_open, sqlite3_exec, slite3_close

Source: Internet
Author: User
Tags sqlite db

Use sqlite3_open to open a database

Prototype:

 
IntSqlite3_open (Const Char* Filename,/*Database filename (UTF-8)*/Sqlite3** Ppdb/*Out: SQLite dB handle*/);

Use this function to start database operations. Two parameters are required. One is the database file name, for example, E:/test. DB. The file name does not need to exist. If the file does not exist, SQLite will automatically create it. If it exists, try to open it as a database file. The second is sqlite3 **, which is the key data structure mentioned above. Do not worry about the underlying details of this structure.
The Return Value of the function indicates whether the operation is correct. If it is sqlite_ OK, the operation is normal. Related return values SQLite defines some macros. For more information about these macros, see the sqlite3.h file. It has a detailed definition (by the way, sqlite3'sCodeThe annotation rate is very high, and it is indeed very high. As long as you can read English, SQLite can help you learn a lot ).

Close the database connection sqlite3_close usage

Prototype:

 
IntSqlite3_close (sqlite3 * ppdb );

Ppdb is the link to the database opened with sqlite3_open.

SQL statement execution sqlite3_exec usage

Prototype:

 Int  Sqlite3_exec (sqlite3 * Ppdb, /*  An Open Database */    Const   Char * SQL, /*  SQL to be evaluated  */    Int (* Callback )( Void *, Int , Char **, Char **), /*  Callback Function  */    Void *, /*  1st argument to callback  */    Char ** Errmsg /*  Error MSG written here  */  ); 

This is the function used to execute an SQL statement.
The first parameter is a pointer obtained by the open function. It is the key data structure.
The 2nd constchar * SQL is an SQL statement ending with \ 0.
The 3rd sqlite3_callback parameter is a callback. After this statement is executed, sqlite3 will call the function you provided.
The third parameter void * is the pointer you provide. You can pass any pointer parameter here. This parameter will eventually be passed to the callback function. If you do not need to pass the pointer to the callback function, you can enter null. Let's take a look at the write of the callback function.
And the use of this parameter.
The first parameter char ** errmsg is the error message. Note the pointer. Sqlite3 contains many fixed error messages. After sqlite3_exec is executed, you can view this pointer when execution fails (cout <errmsg gets a string of information, which tells you where the error is. The sqlite3_exec function points the pointer you provided to the error message by modifying the pointer you passed in, so that the sqlite3_exec function can use this char * to get a specific error message.

Note: Normally, either sqlite3_callback or void * after it can be null. If this parameter is set to null, callback is not required. For example, if you perform insert and delete operations, there is no need to use callback. When you do select, you need to use a callback, because sqlite3 checks the data and uses a callback to tell you what data has been found.

Exec callback

Typedef int (* sqlite3_callback) (void *, Int, char **, char **);
Your callback function must be defined as the type of the above function. The following is a simple example:
// Sqlite3 callback function
// Each time SQLite finds a record, this callback is called once.
Int loadmyinfo (void * para, intn_column, char ** column_value, char ** column_name );

// Para is the void * parameter you pass in sqlite3_exec. Through the Para parameter, you can pass in some special pointers (such as class pointers and structure pointers ),
// Then forcibly convert the data type to the corresponding type (the type is void *, which must be converted to your type to be available ). Then operate on the data
// N_column indicates the number of fields in the record (that is, the number of columns in the record)
// Char ** column_value is a key value. The retrieved data is saved here. It is actually a one-dimensional array (not a two-dimensional array ),
// Each element is a char * value and a field content (represented by a string and ended with \ 0)
// Char ** column_name corresponds to column_value, indicating the field name of this field

Instance:

# Include <iostream> Using   Namespace  STD; # include  "  SQLite/sqlite3.h  "  Int Callback ( Void *, Int ,Char **, Char ** );  Int  Main () {sqlite3 * DB;  Int Nresult = sqlite3_open ( "  Test. DB  " ,& DB );  If (Nresult! = Sqlite_ OK) {cout < " Failed to open database:  " <Sqlite3_errmsg (db) < Endl;  Return   0  ;}  Else  {Cout < "  Database opened successfully  " < Endl ;}  Char * Errmsg; nresult = Sqlite3_exec (dB,"  Create Table fuck (ID integer primary key autoincrement, name varchar (100 ))  " , Null, null ,& Errmsg );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout < Errmsg; sqlite3_free (errmsg );  Return   0  ;}  String  Strsql; strsql + = " Begin; \ n  "  ;  For ( Int I = 0 ; I < 100 ; I ++ ) {Strsql + = "  Insert into fuck values (null, 'heh'); \ n  "  ;} Strsql + = "  Commit;  " ;  //  Cout <strsql <Endl;  Nresult = Sqlite3_exec (dB, strsql. c_str (), null, null ,& Errmsg );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg );  Return   0  ;} Strsql = " Select * from fuck  "  ; Nresult = Sqlite3_exec (dB, strsql. c_str (), callback, null ,& Errmsg );  If (Nresult! = Sqlite_ OK) {sqlite3_close (db); cout <Errmsg < Endl; sqlite3_free (errmsg );  Return   0  ;} Sqlite3_close (db );  Return   0  ;} Int Callback ( Void *, Int Ncount, Char ** Pvalue, Char ** Pname ){  String  S;  For ( Int I = 0 ; I <ncount; I ++ ) {S + = Pname [I]; S + ="  :  "  ; S + = Pvalue [I]; S + = "  \ N  "  ;} Cout <S < Endl;  Return   0  ;} 

Modified fromDong chunguang

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.