2010-10-18 124 views

回答

8
+1

感謝。我沒有注意到QuickStart教程,而我自己瀏覽sqlite網站。看看我需要什麼:簡單的說明如何開始使用SQLite,而不是任何高級行話:) – zaplec 2010-10-18 17:42:06

+5

歡迎來到堆棧溢出!而不是鏈接,而是希望將實際來源包含在答案正文中。 – Jonny 2015-10-18 08:22:26

+1

請考慮在此答案中添加更多的細節。 – NathanOliver 2017-05-08 18:50:17

8

如何關於 'An Introduction to Sqlite C/C++ Interface',並且有一個整體C++示例here on CodeProject

這是更加充滿樣品的比特,

#include "CppSQLite.h" 
#include <ctime> 
#include <iostream> 
using namespace std; 
const char* gszFile = "C:\\test.db"; 

int main(int argc, char** argv) 
{ 
    try 
    { 
     int i, fld; 
     time_t tmStart, tmEnd; 
     CppSQLiteDB db; 

     cout << "SQLite Version: " << db.SQLiteVersion() << endl; 

     db.open(gszFile); 
     cout << db.execScalar("select count(*) from emp;") 
       << " rows in emp table in "; 
     db.Close(); 
    } 
    catch (CppSQLiteException& e) 
    { 
     cerr << e.errorCode() << ":" << e.errorMessage() << endl; 
    } 
} 
6

使用SQLite讀阿例如:

#include <stdio.h> 
#include <sqlite3.h> 
#include <string.h> 


int main(int argc, char** argv) 
{ 
    const char*   username = "satyam"; 
    char     q[999]; 
    sqlite3*    db; 
    sqlite3_stmt*  stmt; 
    int     row = 0; 
    int     bytes; 
    const unsigned char* text; 

    if (2 == argc) { 
     username = argv[1]; 
    } 

    q[sizeof q - 1] = '\0'; 
    snprintf(
     q, 
     sizeof q - 1, 
     "SELECT ipaddr FROM items WHERE username = '%s'", 
     username 
    ); 

    if (sqlite3_open ("test.db", &db) != SQLITE_OK) { 
     fprintf(stderr, "Error opening database.\n"); 
     return 2; 
    } 

    printf("Query: %s\n", q); 

    sqlite3_prepare(db, q, sizeof q, &stmt, NULL); 

    bool done = false; 
    while (!done) { 
     printf("In select while\n"); 
     switch (sqlite3_step (stmt)) { 
     case SQLITE_ROW: 
      bytes = sqlite3_column_bytes(stmt, 0); 
      text = sqlite3_column_text(stmt, 1); 
      printf ("count %d: %s (%d bytes)\n", row, text, bytes); 
      row++; 
      break; 

     case SQLITE_DONE: 
      done = true; 
      break; 

     default: 
      fprintf(stderr, "Failed.\n"); 
      return 1; 
     } 
    } 

    sqlite3_finalize(stmt); 

    return 0; 
} 
+0

這是一個糟糕的格式化,錯誤的排序,未使用的變量,無意義的臨時對象,缺少換行符等等的完整混亂。我開始修復它,但後來意識到這不是我的責任。我建議新手讀者不要從這些代碼中獲得靈感。此外,我認爲這是新手讀者誰upvoting,在這種情況下,你不應該。 – 2017-07-14 12:20:51

0

一種方式做到這一點而無需額外的包裝

#include <stdio.h> 
#include <string> 
using std::string; 
#include <sstream> 
using std::stringstream; 

#include "sqlite3.h" 

bool find_employee(int _id) 
{ 
    bool found = false; 
    sqlite3* db; 
    sqlite3_stmt* stmt; 
    stringstream ss; 

    // create sql statement string 
    // if _id is not 0, search for id, otherwise print all IDs 
    // this can also be achieved with the default sqlite3_bind* utilities 
    if(_id) { ss << "select * from employees where id = " << _id << ";"; } 
    else { ss << "select * from employees;"; } 
    string sql(ss.str()); 

    //the resulting sql statement 
    printf("sql: %s\n", sql.c_str()); 

    //get link to database object 
    if(sqlite3_open("data/test.db", &db) != SQLITE_OK) { 
     printf("ERROR: can't open database: %s\n", sqlite3_errmsg(db)); 
     sqlite3_close(db); 
     return found; 
    } 

    // compile sql statement to binary 
    if(sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, NULL) != SQLITE_OK) { 
     printf("ERROR: while compiling sql: %s\n", sqlite3_errmsg(db)); 
     sqlite3_close(db); 
     sqlite3_finalize(stmt); 
     return found; 
    } 

    // execute sql statement, and while there are rows returned, print ID 
    int ret_code = 0; 
    while((ret_code = sqlite3_step(stmt)) == SQLITE_ROW) { 
     printf("TEST: ID = %d\n", sqlite3_column_int(stmt, 0)); 
     found = true; 
    } 
    if(ret_code != SQLITE_DONE) { 
     //this error handling could be done better, but it works 
     printf("ERROR: while performing sql: %s\n", sqlite3_errmsg(db)); 
     printf("ret_code = %d\n", ret_code); 
    } 

    printf("entry %s\n", found ? "found" : "not found"); 

    //release resources 
    sqlite3_finalize(stmt); 
    sqlite3_close(db); 

    return found; 
}