【正文】
if (sqlite3_step(stmt5) == SQLITE_DONE) { printf(The test table has been dropped.\n)。 } sqlite3_finalize(stmt5)。 sqlite3_close(conn)。 } int main() { doTest()。 return 0。 } //輸出結(jié)果如下: //Succeed to create test table now. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //Insert Succeed. //The test table has been dropped.數(shù)據(jù)查詢是每個關(guān)系型數(shù)據(jù)庫都會提供的最基本功能,下面的代碼示例將給出如何通過SQLite API獲取數(shù)據(jù)。 1). 創(chuàng)建測試數(shù)據(jù)表。 2). 插入一條測試數(shù)據(jù)到該數(shù)據(jù)表以便于后面的查詢。 3). 執(zhí)行SELECT語句檢索數(shù)據(jù)。 4). 刪除測試表。include include string include using namespace std。 void doTest() { sqlite3* conn = NULL。 //1. 打開數(shù)據(jù)庫 int result = sqlite3_open(D:/,amp。conn)。 if (result != SQLITE_OK) { sqlite3_close(conn)。 return。 } const char* createTableSQL = CREATE TABLE TESTTABLE (int_col INT, float_col REAL, string_col TEXT)。 sqlite3_stmt* stmt = NULL。 int len = strlen(createTableSQL)。 //2. 準(zhǔn)備創(chuàng)建數(shù)據(jù)表,如果創(chuàng)建失敗,需要用sqlite3_finalize釋放sqlite3_stmt對象,以防止內(nèi)存泄露。 if (sqlite3_prepare_v2(conn,createTableSQL,len,amp。stmt,NULL) != SQLITE_OK) { if (stmt) sqlite3_finalize(stmt)。 sqlite3_close(conn)。 return。 } //3. 通過sqlite3_step命令執(zhí)行創(chuàng)建表的語句。對于DDL和DML語句而言,sqlite3_step執(zhí)行正確的返回值 //只有SQLITE_DONE,對于SELECT查詢而言,如果有數(shù)據(jù)返回SQLITE_ROW,當(dāng)?shù)竭_(dá)結(jié)果集末尾時則返回 //SQLITE_DONE。 if (sqlite3_step(stmt) != SQLITE_DONE) { sqlite3_finalize(stmt)。 sqlite3_close(conn)。 return。 } //4. 釋放創(chuàng)建表語句對象的資源。 sqlite3_finalize(stmt)。 printf(Succeed to create test table now.\n)。 //5. 為后面的查詢操作插入測試數(shù)據(jù)。 sqlite3_stmt* stmt2 = NULL。 const char* insertSQL = INSERT INTO TESTTABLE VALUES(20,39。this is a test.39。)。 if (sqlite3_prepare_v2(conn,insertSQL,strlen(insertSQL),amp。stmt2,NULL) != SQLITE_OK) { if (stmt2) sqlite3_finalize(stmt2)。 sqlite3_close(conn)。 return。 } if (sqlite3_step(stmt2) != SQLITE_DONE) { sqlite3_finalize(stmt2)。 sqlite3_close(conn)。 return。 } printf(Succeed to insert test data.\n)。 sqlite3_finalize(stmt2)。 //6. 執(zhí)行SELECT語句查詢數(shù)據(jù)。 const char* selectSQL = SELECT * FROM TESTTABLE。 sqlite3_stmt* stmt3 = NULL。 if (sqlite3_prepare_v2(conn,selectSQL,strlen(selectSQL),amp。stmt3,NULL) != SQLITE_OK) { if (stmt3) sqlite3_finalize(stmt3)。 sqlite3_close(conn)。 return。 } int fieldCount = sqlite3_column_count(stmt3)。 do { int r = sqlite3_step(stmt3)。 if (r == SQLITE_ROW) { for (int i = 0。 i fieldCount。 ++i) { //這里需要先判斷當(dāng)前記錄當(dāng)前字段的類型,再根據(jù)返回的類型使用不同的API函數(shù) //獲取實際的數(shù)據(jù)值。 int vtype = sqlite3_column_type(stmt3,i)。 if (vtype == SQLITE_INTEGER) { int v = sqlite3_column_int(stmt3,i)。 printf(The INTEGER value is %d.\n,v)。 } else if (vtype == SQLITE_FLOAT) { double v = sqlite3_column_double(stmt3,i)。 printf(The DOUBLE value is %f.\n,v)。 } else if (vtype == SQLITE_TEXT) { const char* v = (const char*)sqlite3_column_text(stmt3,i)。 printf(The TEXT value is %s.\n,v)。 } else if (vtype == SQLITE_NULL) { printf(This value is NULL.\n)。 } } } else if (r == SQLITE_DONE) { printf(Select Finished.\n)。 break。 } else { printf(Failed to SELECT.\n)。 sqlite3_finalize(stmt3)。 sqlite3_close(conn)。 return。 } } while (true)。 sqlite3_finalize(stmt3)。 //7. 為了方便下一次測試運行,我們這里需要刪除該函數(shù)創(chuàng)建的數(shù)據(jù)表,否則在下次運行時將無法 //創(chuàng)建該表,因為它已經(jīng)存在。 const char* dropSQL = DROP TABLE TESTTABLE。 sqlite3_stmt* stmt4 = NULL。 if (sqlite3_prepare_v2(conn,dropSQL,strlen(dropSQL),amp。stmt4,NULL) != SQLITE_OK) { if (stmt4) sqlite3_finalize(stmt4)。 sqlite3_close(conn)。 return。 } if (sqlite3_step(stmt4) == SQLITE_DONE) { printf(The test table has been dropped.\n)。 } sqlite3_finalize(stmt4)。 sqlite3_close(conn)。 } int main() { doTest()。 return 0。 } //輸出結(jié)果如下: //Succeed to create test table now. //Succeed to insert test data. //The INTEGER value is 20. //The DOUBLE value is . //The TEXT value is this is a test.. //Select Finished. //The test table has been dropped.SQLite C/C++ 接口簡介This article provides an overview and roadmap to the C/C++ interface to SQLite.Early versions of SQLite were very easy to learn since they only supported 5 C/C++ interfaces. But as SQLite has grown in capability, new C/C++ interfaces have been added so that now there are over 185 distinct APIs. This can be overwhelming to a new programmer. Fortunately, most of the C/C++ interfaces in SQLite are very specialized and never need to be used. Despite having so many entry points, the core API is still relatively simple and easy to code to. This article aims to provide all of the background information needed to easily understand how SQLite works.A separate document,The SQLite C/C++ Interface, provides detailed specifications for all of the various C/C++ APIs for SQLite. Once the reader understands the basic principles of operation for SQLite,that documentshould be used as a reference guide. This article is intended as introduction only and is neither a plete nor authoritative reference for the SQLite API. Core Objects And InterfacesThe principal task of an SQL database engine is to evaluate statements of SQL. In order to acplish this purpose, the developer needs to know about two objects: Thedatabase connectionobject: sqlite3 Theprepared statementobject: sqlite3_stmtStrictly speaking, theprepared statementobject is not required since the convenience wrapper interfaces,sqlite3_execorsqlite3_get_table, can be used and these convenience wrappers encapsulate and hide theprepared statementobject. Nevertheless, an understanding ofprepared statementsis needed to make full use of SQLite.Thedatabase connectionandprepared statementobjects are controlled by a small set of C/C++ interface routine listed below. sqlite3_open() sqlite3_prepare() sqlite3_step() sqlite3_column() sqlite3_finalize() sqlite3_close()The six C/C++ interface routines an