SQLite C++ 封裝:easySQLite

jopen 9年前發布 | 34K 次閱讀 SQLite 數據庫服務器 easySQLite

一個簡單的 SQLite C++ 封裝.

優勢:

  • 優雅的面向對象解決方案

  • 顯式命名和調用

  • 使用異常以及方法返回值

  • 容易理解

  • 靈活而且可擴展

  • 經過強測試

//define table structure
Field definition_tbPerson[] = 
{
        Field(FIELD_KEY),
        Field("fname", type_text, flag_not_null),
        Field("lname", type_text, flag_not_null),
        Field("birthdate", type_time),
        Field(DEFINITION_END),
};

//define database object
sql::Database db;

try
{
        //open database file
        db.open("test.db");

        //define table object
        Table tbPerson(db.getHandle(), "person", definition_tbPerson);

        //remove table from database if exists
        if (tbPerson.exists())
                tbPerson.remove();

        //create new table
        tbPerson.create();

        //define new record
        Record record(tbPerson.fields());

        //set record data
        record.setString("fname", "Jan");
        record.setString("lname", "Kowalski");
        record.setTime("birthdate", time::now());

        //add 10 records
        for (int index = 0; index < 10; index++)
                tbPerson.addRecord(&record);

        //select record to update
        if (Record* record = tbPerson.getRecordByKeyId(7))
        {
                record->setString("fname", "Frank");
                record->setString("lname", "Sinatra");
                record->setNull("birthdate");

                tbPerson.updateRecord(record);
        }

        //load all records
        tbPerson.open();

        //list loaded records
        for (int index = 0; index < tbPerson.recordCount(); index++)
                if (Record* record = tbPerson.getRecord(index))
                        sql::log(record->toString());

        sql::log("");
        sql::log("ALL OK");

} catch (Exception e) {
        printf("ERROR: %s\r\n", e.msg().c_str());
}

項目主頁:http://www.baiduhome.net/lib/view/home/1421724363843

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