Original SQLite's Learning Series Get database version two

Source: Internet
Author: User
Tags sqlite database

This series of articles mainly uses the C + + language to invoke its API to achieve the purpose of glimpse. In addition, the development environment used in this document is Mac + Clion and is developed based on SQLite 3.7.14来.

  One, to download Sqlite-amalgamation-3071400.zip, and then extract to the folder (its structure directory tree as follows):

    .

├──shell.c

├──sqlite3.c

├──sqlite3.h

└──sqlite3ext.h

Second, create a new Sql_tutorial project, King sqlite3.c and Sqlite3.h files copied to the SQL_SRC directory:

    .

├──main.cpp

└──sql_src

├──sqlite3.c

└──sqlite3.h

  

Third, the code that calls SQLite in Main.cpp is as follows:

    

/** * We Get the version of the SQLite database. This time we'll use the an SQL query. */#include<iostream>#include"./sql_src/sqlite3.h"using namespacestd;intMain () {Sqlite3 *db;SQLITE3_STMT *Res; Const Char*file ="test.db";    intrc = Sqlite3_open (file, &db); if(RC! =SQLITE_OK) {cout<<"cannot open database:"<< sqlite3_errmsg (db) <<Endl;        Sqlite3_close (DB); Exit (1); }    rc = SQLITE3_PREPARE_V2 (db,"SELECT sqlite_version ()", -1, &res,0); if(RC! =SQLITE_OK) {cout<<"Failed to fetch data:"<< sqlite3_errmsg (db) <<Endl;        Sqlite3_close (DB); Exit (1); }    rc =Sqlite3_step (RES); if(rc = =Sqlite_row) {cout<< Sqlite3_column_text (Res,0) <<Endl; }    sqlite3_finalize (res);sqlite3_close (DB); return 0;}

The results of the final operation are as follows:

  

3.7.  -  0

  

Call Analysis:

Sqlite3 *db;

1. The structure of the sqlite3 defines a database handle. Each open SQLite database is represented by a database handle.

Sqlite3_stmt *res;

2, the SQLITE3_STMT structure represents an SQL statement.

int rc = sqlite3_open (file, &db);

3. The Sqlite3_open () function opens a new database connection. Its parameters are the database name and the database handle. In "Test.db", a special database name is used to open a memory database. The return code of the function indicates whether the database was opened successfully. When the connection is successfully established, the SQLITE_OK is returned.

if (RC! = Sqlite_ok)        {"" << sqlite3_errmsg (db) << Endl;        Sqlite3_close (db);        Exit (1);    }

4. If the return code indicates an error, we print the message to the console, close the database processing, and terminate the plan. The sqlite3_errmsg () function returns a description of the error. If an error occurs when it is opened, the resource associated with the database connection handle should be freed by making it to the Sqlite3_close () function.

" SELECT sqlite_version () ",-10);     if (RC! = Sqlite_ok)        {"" << sqlite3_errmsg (db) << endl;< C14/>sqlite3_close (db);        Exit (1);    }

5. Before executing the SQL statement, you must call one of the Sqlite3_prepare functions that were first compiled into byte code. (The Sqlite3_prepare () function is deprecated.) )

The SQLITE3_PREPARE_V2 () function has five parameters.

The first parameter is a database handle obtained from the Sqlite3_open () function.

The second parameter is the SQL statement to compile.

The third parameter is the maximum length of the SQL statement in bytes. Passing 1 causes the SQL string to be read to the end of the first 0 terminator substring. Depending on the file, you can get some small performance benefits by passing the exact number of bytes provided by the SQL string.

The fourth argument is a statement handle. This will point to the precompiled statement if SQLITE3_PREPARE_V2 () runs successfully

The last parameter is a pointer to an unused part of the SQL statement. Only the first statement of the SQL string is compiled, so the parameters point to what remains to be compiled. We passed in 0 because this parameter is not very important to us.

SQLITE3_PREPARE_V2 () returns SQLITE_OK if successful, otherwise the error code returns

  

rc = Sqlite3_step (res);

6. Call Sqlite3_step () to run the SQL statement. The Sqlite_row return code indicates that another line is ready. Our SQL statement returns only one row of data, so we call this function once.

  

Sqlite3_finalize (RES);

7. The sqlite3_finalize () function destroys the prepared statement object.

Sqlite3_close (DB);

8. The Sqlite3_close () function closes the database connection.

  

Original SQLite's Learning Series Get database version two

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.