用C++簡單封裝了一下SQLite

jopen 12年前發布 | 19K 次閱讀 SQLite C/C++開發 C/C++

SQLite 強大,大家都知道,但是原生是C接口,用來其應用層稍微麻煩些。

今天做了個簡單的C++封裝,使用面向對象的方法來使用。

優點:面向對象的方式,減少應用層的復雜性;

缺點:暫對多線程環境支持不足。

 

使用樣例如下:

// a simple example to use SQLitePP
void simple_example()
{
    //define a connection
    SQLitePP::DBConnection conn;

    // connect to a database
    int rc = conn.connect("D:\\mytestdb.db");
    if (rc != 0) {
        std::cout << conn.getErrorMessage() << std::endl;
        return;
    }

    // define a command and set a command text
    SQLitePP::DBCommand cmd(&conn);
    cmd.setCommandText("create table tbl_test(id integer not null, name text, contact text)");
    rc = cmd.execute(); // execute
    if (rc != 0) {
        std::cout << conn.getErrorMessage() << std::endl;
        return;
    }

    // insert data
    cmd.setCommandText("insert into tbl_test(id, name, contact) values(0, 'gavin', 'vxling@gmail.com')");
    rc = cmd.execute(); // execute
    if (rc != 0) {
        std::cout << conn.getErrorMessage() << std::endl;
        return;
    }

    // execute a query
    cmd.setCommandText("select * from tbl_test");
    rc = cmd.execute(); // execute
    if (rc != 0) {
        std::cout << conn.getErrorMessage() << std::endl;
        return;
    }

    // print all result
    while (cmd.fetchNext()) {
        std::cout << cmd.field(0).toInt() << ", " << cmd.field(1) << std::endl;
    }
}

可以使用內存數據庫、文件數據庫等多種方式,這里的例子比較簡單。


SQLite地址:http://sqlite.org/

SQLitePP 下載地址:https://sourceforge.net/projects/sqlite4pp/ 目前只有 VC2010 版本的庫。

轉自:http://blog.csdn.net/lonelyforest/article/details/7706902

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!