首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式碼之家 >> 編程語言 >  >> C語言 >> C++ >> 關於C++

C++ 操作 SQLite3:CppSQLite3 封裝庫完整使用指南

     編輯:關於C++

使用 CppSQLite3 後操作 SQLite3 更加方便。以下提供完整原始碼與使用範例。

CppSQLite3.h

// 完整頭文件內容見原文,包含:
// CppSQLite3Exception 異常類別
// CppSQLite3Buffer 格式化緩衝區
// CppSQLite3Binary 二進位資料處理
// CppSQLite3Query 查詢結果集
// CppSQLite3Table 表格結果集
// CppSQLite3Statement 預編譯陳述式
// CppSQLite3DB 資料庫連線管理

CppSQLite3.cpp

// 完整實作內容見原文,包含所有類別方法的具體實作,
// 以及 SQLite 編碼/解碼二進位資料的輔助函式。

使用方法範例

#include 
#include "CppSQLite3.h"
#include 
using namespace std;

const char* gszFile = "e:\\sqlite3\\test.db";

void main1() {
    cout << "hello sqlite3" << endl;
    try {
        CppSQLite3DB db;
        char* sql;
        cout << "SQLite Header Version: " << CppSQLite3DB::SQLiteHeaderVersion() << endl;
        cout << "SQLite Library Version: " << CppSQLite3DB::SQLiteLibraryVersion() << endl;
        cout << "SQLite Library Version Number: " << CppSQLite3DB::SQLiteLibraryVersionNumber() << endl;

        db.open(gszFile);
        cout << endl << "emp table exists=" << (db.tableExists("person") ? "TRUE" : "FALSE") << endl;

        // 建立資料表
        sql = "CREATE TABLE person("
              "id INT PRIMARY KEY NOT NULL,"
              "name TEXT NOT NULL,"
              "age INT NOT NULL,"
              "address CHAR(50),"
              "salary REAL );";
        // db.execDML(sql);

        // 插入資料
        sql = "insert into person values (1, 'David Beckham',34,'china person',3455.78);"
              "insert into person values (2, 'changshuhang',33,'china person1',3455.34);"
              "insert into person values (3, 'houyuzhu',37,'china person2',34534);"
              "insert into person values (4, 'chengye',356,'china person3',45.78);"
              "insert into person values (5, 'dujianfei',38,'china person4',3655.78);";
        int nRows = db.execDML(sql);
        cout << nRows << " rows inserted" << endl;

        // 查詢資料
        CppSQLite3Query q = db.execQuery("select * from person order by id;");
        for (int fld = 0; fld < q.numFields(); fld++) {
            cout << q.fieldName(fld) << "(" << q.fieldDeclType(fld) << ")|";
        }
        cout << endl;
        while (!q.eof()) {
            cout << q.fieldValue(0) << "|"
                 << q.fieldValue(1) << "|"
                 << q.fieldValue(2) << "|"
                 << q.fieldValue(3) << endl;
            q.nextRow();
        }

        // 取得整個表格
        CppSQLite3Table t = db.getTable("select * from person order by id;");
        for (int fld = 0; fld < t.numFields(); fld++) cout << t.fieldName(fld) << "|";
        cout << endl;
        for (int row = 0; row < t.numRows(); row++) {
            t.setRow(row);
            for (int fld = 0; fld < t.numFields(); fld++) {
                if (!t.fieldIsNull(fld)) cout << t.fieldValue(fld) << "|";
                else cout << "NULL|";
            }
            cout << endl;
        }
    } catch (CppSQLite3Exception& e) {
        cerr << e.errorCode() << ":" << e.errorMessage() << endl;
    }

    char c(' ');
    while (c != 'q' && c != 'Q') {
        cout << "Press q then enter to quit: ";
        cin >> c;
    }
}

CppSQLite3 將 SQLite3 C API 封裝為直觀的 C++ 類別,支援異常處理、預編譯陳述式、參數綁定、二進位資料、事務控制等功能,是 C++ 專案嵌入 SQLite 的優選方案。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式碼之家 All Rights Reserved