基於sqlitecpp的sqlite3 c++封裝


Github: 人富水也甜

感謝GitHub大佬: 

sqlitecpp github:  https://github.com/SRombauts/SQLiteCpp  

sqlite: https://www.sqlite.org/index.html

 

0、特別說明:

  1、因為封裝中引入了 c++11 的相關特性,請選擇  支持c++11及以上的編譯器。  

  2、目前,我是用的sqlite 版本: sqlite-amalgamation-3320300

  3、 目前,封裝基於以下的環境: 

1. Visual Studio
    A. Microsoft Visual Studio Enterprise 2015
    B. version- 14.0.25431.01 Update 3
    C. Microsoft .NET Framework
    D. version-4.8.03752

2.OS: 
    A. Microsoft Windows 10 Professional
    B. version-    10.0.18363 version 18363

 

 

1、為什么要做這個封裝

  A、截至目前,閱讀 SQLitecpp    的源碼后 會發現,CMakeLists.txt 沒有生成動態庫的配置, 我重新配置CMakeLists.txt, 生成了動態庫,但是沒有導出接口,自然 沒有 適用 windows下的lib文件。 

  B、 團隊合作,難免環境不太一樣,而且還要考慮兼容和代碼維護, 靜態庫 的確不適合我目前的需求。 

  C、 源碼編譯到項目,當自己有所變更,則需要將源碼文件提供給調用者,這就更麻煩了。 每次有所變更都需要重新編譯,維護工作量可想而知。

  D、 保護“核心模塊”。

  E、 我喜歡用sqlite數據庫作為項目的配置文件, 管理方便,個人經驗: 相比I NI 和 XML 好多了。 

  F、 2年前就寫過基於sqlite3的源碼讀寫封裝,但是,存放在了公司,公司不支持導出。 哈哈, 自己手上沒有, 當時 還用 QT 做了 基於sqlite數據庫的配置工具(可以在這里下載源碼: https://github.com/mohistH/sqlite3_database_helper  ), 這個工具是自己寫好導入公司的,手頭有原件。

 

2、提交到github

  A、截至目前,我之做了 win10下的動態庫,晚點會補上 CMake + MAC 的更新。 

  B、 動態庫提供的接口基本上都做了測試,但是, 肯定有遺漏,  歡迎 留言指正。

  C、注釋的內容可能看不懂(英語太菜) ,但是,正在努力完善。

  D、項目地址: https://github.com/mohistH/SQLiteCpp

 

3、接口說明

  目前,我自己導出了以下接口,可繼續擴展。

  A、初始化, 支持 sqlite_open 和 sqlite_open_v2

        /* @ brief: initial some params, and it will call sqlite_open_v2 to open db file
        *  @ const std::string & db_file - where is the .db file. such as: "c:/sqlite/demo.db"
        *  @ const sqlite3_open_with open_type - 1:read_only, 2 - read_write, 4 - create, if this param cannot be found the definition, it will reset 1;
        *  @ const int busy_time_out - busy time out, default is 0;
        *  @ const std::string & vfs - // std::string& out_str_exception
        *  @ std::string& out_str_exception - error msg. out_str_exception will record the error information if initial database occur error
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - db_file is empty
                2 - db_file is not exist
                3 - internal error, create _pdatabase object failed
                4 - initial error, check the param [out_str_exception] to get error information
        */
        int init_v2(const std::string db_file, std::string& out_str_exception, const sqlite3_open_with open_type = OPEN_READONLY, const int busy_time_out = 0, const std::string& vfs = "");
        // this function will call sqlite_open to open db file
        int init(const std::string db_file, std::string& out_str_exception, const sqlite3_open_with open_type = OPEN_READONLY);

  B、 釋放, 調用 下面的函數完成 動態庫內部的自源釋放

       int uninit();

   C、 查詢表是否存在

int table_exist(const std::string table_name);

  D、 快速獲取 表中某個數據

int get_single_value(const std::string str_query, std::string& str_result, std::string& out_str_exception);

  E、獲取當前操作的數據庫文件, 含文件全路徑

int get_db_file_name(std::string& str_db_file_name, std::string& out_str_exception);

  F、 查詢

     1)、查詢表的某1列的名稱

int get_table_column_name(const unsigned int in_column_index, std::string& out_str_name, std::string& out_str_exception);

     2)、 重置statement

int reset(std::string& out_str_exception);

    3)、查詢表一共有幾列

int get_table_column_count(unsigned int& out_total_column_count, std::string& out_str_err);

    4)、 查詢對name對應的索引

int get_index(const std::string name, unsigned int& out_index);

    5)、 下面是關於查詢需要用到的綁定,提供了相應的接口

        int bind(const int in_index, const int in_value, std::string& out_str_exception);

        int bind(const int in_index, const unsigned  in_value, std::string& out_str_exception);

        int bind(const int in_index, const long  in_value, std::string& out_str_exception);

        int bind(const int in_index, const long long in_value, std::string& out_str_exception);
        
        int bind(const int in_index, const double   in_value, std::string& out_str_exception);

        int bind(const int in_index, const std::string  in_value, std::string& out_str_exception);

        int bindNoCopy(const int in_index, const std::string in_value, std::string& out_str_exception);

        int bind(const int in_index, std::string& out_str_exception);

        int bind(const std::string name, const int in_value, std::string& out_str_exception);

        int bind(const std::string name, const unsigned   in_value, std::string& out_str_exception);

        int bind(const std::string name, const long   in_value, std::string& out_str_exception);

        int bind(const std::string name, const long long       in_value, std::string& out_str_exception);

        int bind(const std::string name, const double          in_value, std::string& out_str_exception);

        int bind(const std::string name, const std::string    in_value, std::string& out_str_exception);

        int bindNoCopy(const std::string name, const std::string in_value, std::string& out_str_exception);

        int bind(const std::string name, std::string& out_str_exception); 

    6)、 解除綁定,與上面 5) 對應使用

int clear_bindinds(std::string& out_str_exception);

    7)、 執行 查詢sql語句專用函數。 必須要調用這個函數,調用  1)  2) 3) 4) 5) 6) 中的函數才能有值

int exec_query_sql(const std::string in_str_query_sql, std::string& out_str_exception);

    8)、 對表的管理: 創建、刪除、更新、插入 則需要使用下面的函數:

int exec_db_sql(const std::string in_str_db_sql, int& out_result_row, std::string& out_str_exception);

    9)、 查詢數據時使用

bool exec_query_step();
int exec_query_step(std::string& out_str_exception);

  

    10)、 返回查詢結果對應的類型

        int get_column_int(const unsigned int in_column_index,        int& out_val,            std::string& out_str_exception);
        int get_column_double(const unsigned int in_column_index,    double& out_val,        std::string& out_str_exception);
        int get_column_uint(const unsigned int in_column_index,        unsigned int& out_val,    std::string& out_str_exception);
        int get_column_int64(const unsigned int in_column_index,    long long& out_val,        std::string& out_str_exception);
        int get_column_string(const unsigned int in_column_index,    std::string& out_val,    std::string& out_str_exception);

    11) 、事務

int commit(std::string& out_str_exception);

    12)、 查詢可執行程序當前所在路徑,例如: C;\\user\\demo  

static std::string get_exec_path();

 

4、接口頭文件源碼

#ifndef _hsqlite_db_h
#define _hsqlite_db_h


#include <iostream>
#include <memory>

#if defined (_WIN32) 

    #ifndef _sqlite_db_api_export_
        #define _sqlite_db_api_export_    __declspec(dllexport)
    #else
        #define _sqlite_db_api_export_    __declspec(dllimport)
    #endif // !_sqlite_db_api_export_

#else
    
    #define _sqlite_db_api_export_ __attribute__((visibility ("default")))
#endif // !



namespace sqlite_db
{

    class hsqlite_db_imp;

    /*
    *    @ brief: a helper to use sqlite database. It bases on SQLitecpp , 
                which is an openning source on github [https://github.com/SRombauts/SQLiteCpp]
    */
    class _sqlite_db_api_export_ hsqlite_db
    {

    public:
        // 
        enum sqlite3_open_with
        {
            OPEN_READONLY         = 0x00000001,  /* Ok for sqlite3_open_v2() */
            OPEN_READWRITE       = 0x00000002,  /* Ok for sqlite3_open_v2() */
            OPEN_CREATE          = 0x00000004,  /* Ok for sqlite3_open_v2() */
            OPEN_DELETEONCLOSE   = 0x00000008,  /* VFS only */
            OPEN_EXCLUSIVE       = 0x00000010,  /* VFS only */
            OPEN_AUTOPROXY       = 0x00000020,  /* VFS only */
            OPEN_URI             = 0x00000040,  /* Ok for sqlite3_open_v2() */
            OPEN_MEMORY          = 0x00000080,  /* Ok for sqlite3_open_v2() */
            OPEN_MAIN_DB         = 0x00000100,  /* VFS only */
            OPEN_TEMP_DB         = 0x00000200,  /* VFS only */
            OPEN_TRANSIENT_DB    = 0x00000400,  /* VFS only */
            OPEN_MAIN_JOURNAL    = 0x00000800,  /* VFS only */
            OPEN_TEMP_JOURNAL    = 0x00001000,  /* VFS only */
            OPEN_SUBJOURNAL      = 0x00002000,  /* VFS only */
            OPEN_MASTER_JOURNAL  = 0x00004000,  /* VFS only */
            OPEN_NOMUTEX         = 0x00008000,  /* Ok for sqlite3_open_v2() */
            OPEN_FULLMUTEX       = 0x00010000,  /* Ok for sqlite3_open_v2() */
            OPEN_SHAREDCACHE     = 0x00020000,  /* Ok for sqlite3_open_v2() */
            OPEN_PRIVATECACHE    = 0x00040000,  /* Ok for sqlite3_open_v2() */
            OPEN_WAL             = 0x00080000,  /* VFS only */
        };

    public:
        explicit hsqlite_db();

        // it will call  uninit to ensure that _psqlite_db_imp releases
        virtual ~hsqlite_db();

        hsqlite_db(const hsqlite_db &instance) = delete;
        hsqlite_db & operator = (const hsqlite_db &instance) = delete;
        // -------------------------------------------------------------------------------

        // -------------------------------------------------------------------------------
        //
        // initialization
        //
        // -------------------------------------------------------------------------------

        /* @ brief: initial some params, and it will call sqlite_open_v2 to open db file
        *  @ const std::string & db_file - where is the .db file. such as: "c:/sqlite/demo.db"
        *  @ const sqlite3_open_with open_type - 1:read_only, 2 - read_write, 4 - create, if this param cannot be found the definition, it will reset 1;
        *  @ const int busy_time_out - busy time out, default is 0;
        *  @ const std::string & vfs - // std::string& out_str_exception
        *  @ std::string& out_str_exception - error msg. out_str_exception will record the error information if initial database occur error
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - db_file is empty
                2 - db_file is not exist
                3 - internal error, create _pdatabase object failed
                4 - initial error, check the param [out_str_exception] to get error information
        */
        int init_v2(const std::string db_file, std::string& out_str_exception, const sqlite3_open_with open_type = OPEN_READONLY, const int busy_time_out = 0, const std::string& vfs = "");
        // this function will call sqlite_open to open db file
        int init(const std::string db_file, std::string& out_str_exception, const sqlite3_open_with open_type = OPEN_READONLY);



        /* @ brief: call this function to release resource before quitting
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success , the default result is 0;
        */
        int uninit();


        /* @ brief: check if the table_me exists
        *  @ const std::string table_name - table name
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - table is contained in the database
                1 - false, the database doesnt have that table
                2 - failed, the param [tabl_name] is empty
                3 - failed, internal error(_pdatabase is nullptr)
        */
        int table_exist(const std::string table_name);



        /* @ brief: Get a single value result with an easy to use shortcut
        *  @ const std::string str_query - sql string to query, for example, "SELECT value FROM test WHERE id=2"
        *  @ std::string& str_result - the query's result
        *  @ std::string out_str_exception - error msg
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - querry success
                1 - failed, the param [str_query] is empty
                2 - failed, internal error(_pdatabase is nullptr)
        */
        int get_single_value(const std::string str_query, std::string& str_result, std::string& out_str_exception);


        /* @ brief: query the database's name of current operation
        *  @ std::string & str_db_file_name - return the name of db file's name
        *  @ std::string & out_str_exception - save error msg if sth gets wrong
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success, and this is a flag to open db file successfully
                1 - failed, internal error, the object of the db file is nullptr
        */
        int get_db_file_name(std::string& str_db_file_name, std::string& out_str_exception);


        // -------------------------------------------------------------------------------

        /* @ brief: Reset the statement to make it ready for a new execution
        *  @ std::string & out_str_exception - error msg
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, please check [out_str_exception] to get more details
        */
        int reset(std::string& out_str_exception);


        /* @ brief: get column's name
        *  @ const unsigned int & in_column_index - index of column
        *  @ std::string & out_str_name - column's name
        *  @ std::string & out_str_exception - error msg
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - an error occurs, please check [out_str_exception] to get more details
        */
        int get_table_column_name(const unsigned int in_column_index, std::string& out_str_name, std::string& out_str_exception);

        /* @ brief: get total rows of the table.
        *  @ unsigned int & out_total_column_count - the total row count
        *  @ std::string & out_str_exception - error msg
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - an error occur, please check [out_str_exception] to get more details
        */
        int get_table_column_count(unsigned int& out_total_column_count, std::string& out_str_err);

        /* @ brief: get name's index
        *  @ const std::string & name - the name of idex 
        *  @ unsigned int& out_index - the result of index
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - falied, it doesnt initialize db file
                2 - failed, name is empty
        */
        int get_index(const std::string name, unsigned int& out_index);


        ///------------------------ bind start---------------------------------------------

        /* @ brief: Bind an int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *  std::string& out_str_erre -
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const int in_value, std::string& out_str_exception);

        /**  
        * @brief Bind a 32bits unsigned int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const unsigned  in_value, std::string& out_str_exception);

        /**
        * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const long  in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a 64bits int value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const long long in_value, std::string& out_str_exception);


        /** 
        * @brief Bind a double (64bits float) value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const double   in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a string value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, const std::string  in_value, std::string& out_str_exception);


        /** 
        * @brief Bind a text value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * Main usage is with null-terminated literal text (aka in code static strings)
        *
        * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bindNoCopy(const int in_index, const std::string in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a NULL value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * @see clearBindings() to set all bound parameters to NULL.
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const int in_index, std::string& out_str_exception);


        /**   
        * @brief Bind an int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const int in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a 32bits unsigned int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const unsigned   in_value, std::string& out_str_exception);


        /** 
        * @brief Bind a 32bits long value to a parameter "?", "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const long   in_value, std::string& out_str_exception);

        /* @brief Bind a 64bits int value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const long long       in_value, std::string& out_str_exception);

        /* @brief Bind a double (64bits float) value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const double          in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * @note Uses the SQLITE_TRANSIENT flag, making a copy of the data, for SQLite internal use
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, const std::string    in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a string value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * The string can contain null characters as it is binded using its size.
        *
        * @warning Uses the SQLITE_STATIC flag, avoiding a copy of the data. The string must remains unchanged while executing the statement.
        * @ return - int
            -1 - failed, _psqlite_db_imp is nullptr
            0 - success
            1 - failed, it doesnt initialize db file
            2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bindNoCopy(const std::string name, const std::string in_value, std::string& out_str_exception);

        /** 
        * @brief Bind a NULL value to a named parameter "?NNN", ":VVV", "@VVV" or "$VVV" in the SQL prepared statement (in_index >= 1)
        *
        * @see clearBindings() to set all bound parameters to NULL.
        * @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, it doesnt initialize db file
                2 - failed, an error occurs, check [out_str_exception] to get more details
        */
        int bind(const std::string name, std::string& out_str_exception); // bind NULL value

        // ------------- bind end    ----------------------------------------------------



        /* @ brief: clear bindings
        *  @ std::string & out_str_exception - error msg
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed,  it doesnt initialize db file
                2 - failed, an error occurs, please check [out_str_exception] to get more details
        */
        int clear_bindinds(std::string& out_str_exception);




        // -------------------------------------------------------------------------------
        //
        // excutes sql string
        //
        // -------------------------------------------------------------------------------

        /* @ brief: the following function has the these functioms:  create and drop tables, insert and update a row.
        and it will not work with querying data
        *  @ const std::string  in_str_db_sql - sql string
        *  @ std::string & out_str_exception - exceptional string
        *  @ int& out_result_row - Return the number of rows modified by those SQL statements (INSERT, UPDATE or DELETE only)
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                -2 - failed, there is no database file from initialising
                -3 - falied, [in_str_db_sql] is empty
                -4 - failed, internal error, the object to communicate with database file was created failed
        */
        int exec_db_sql(const std::string in_str_db_sql, int& out_result_row, std::string& out_str_exception);



        /* @ brief: query data only
        *  @ const std::string in_str_query_sql - query string of sql
        *  @ std::string & out_str_exception - if an excpetion occurs, you could check the param to get error's detail
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
        */
        int exec_query_sql(const std::string in_str_query_sql, std::string& out_str_exception);


        /* @ brief: you must run the function [exc_query_sql] , then you could call the following function ,
        it will get the next column after  returnning true;
        Note: if the object do not created, it also returns false
        *  @ return - bool
                true 
                false - failed, _psqlite_db_imp is nullptr
                false - it gets end, or dont initialise the db file and call the [exc_query_sql] function
        */
        bool exec_query_step();

        /* @ brief: to query the data from the next column
        *  @ std::string & out_str_exception - if an error occurs, you could check the param [out_str_exception] to get details
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, dont initialise the db file and call the [exc_query_sql] function firstly
                2 - failed, please to check the param out_str_exception to get details
        */
        int exec_query_step(std::string& out_str_exception);

        /*    @ brief: get the column value
        *    @ const unsigned int in_column_index - which column do you wanna get, it starts zero
        *    @ T& out_val - the value
        *    @ std::string& out_str_exception - if an error occurs, check it to get more details
        *    return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, dont initialise the db file and call the [exc_query_sql] function firstly
                2 - failed, an error occured, please check the [out_str_exception] to get details
                3 - failed, it doesnt get any column or in_column_index is out of this range
        */
        int get_column_int(const unsigned int in_column_index,        int& out_val,            std::string& out_str_exception);
        int get_column_double(const unsigned int in_column_index,    double& out_val,        std::string& out_str_exception);
        int get_column_uint(const unsigned int in_column_index,        unsigned int& out_val,    std::string& out_str_exception);
        int get_column_int64(const unsigned int in_column_index,    long long& out_val,        std::string& out_str_exception);
        int get_column_string(const unsigned int in_column_index,    std::string& out_val,    std::string& out_str_exception);


        // -------------------------------------------------------------------------------
        //
        //  transaction
        //
        // -------------------------------------------------------------------------------

        /* @ brief: Commit the transaction
        *  @ std::string & out_str_exception - if an error occurs, check this param to get more details
        *  @ return - int
                -1 - failed, _psqlite_db_imp is nullptr
                0 - success
                1 - failed, do not initialize database file
                2 - failed, an error occurs, please check the [out_str_exception] to get more details
        */
        int commit(std::string& out_str_exception);

    private:
        hsqlite_db_imp *_psqlite_db_imp = nullptr;
        //std::unique_ptr<hsqlite_db_imp > _psqlite_db_imp;

    };





    // -------------------------------------------------------------------------------
    //
    // util's set 
    //
    // -------------------------------------------------------------------------------

    class _sqlite_db_api_export_ util_set
    {
    public:
        enum
        {
            // the max length of path
            path_max_len_256 = 256,
        };

        util_set(const util_set& instance) = delete;
        util_set& operator = (const util_set& instance) = delete;
        // -------------------------------------------------------------------------------
        
        /* @ brief: it will return the path of executables, such as on windows: c:\demo\sqlite3
                    on linux or mac: Users/xx/Desktop/sqlite.
        *  @ return - std::string
                the  executables' path. the max lenth is 255 chars.
        */
        static std::string get_exec_path();


    private:
        explicit util_set();
        virtual ~util_set() {};
    };

}

#endif // !_hsqlite_db_h

 

5、示例:

  A、 測試的表內容:

    B、示例源碼

    unique_ptr<sqlite_db::hsqlite_db> psqlite_db(new sqlite_db::hsqlite_db);

    if (nullptr == psqlite_db)
    {
        std::cout << "error, create psqlite_db failed" << std::endl;
        return 0;
    }

    // get the current executable path    
    std::string str_path = sqlite_db::util_set::get_exec_path();
    str_path += std::string("\\mydatabase.sqlite");
    std::string str_err;

    // 2、inittial 
    int ret_val = psqlite_db->init(str_path, str_err);

#ifndef _use_example_2_

    if (0 != ret_val)
    {
        cout << "initialise database failed, ret = " << ret_val << endl;
    }
    else
    {
        std::string str_query("SELECT * FROM test");
        std::string str_err;

        // 當前表的總列數
        unsigned int total_count_row = 0;
            
        // 執行查詢sql
        int ret_query = psqlite_db->exec_query_sql(str_query, str_err);
        if (0 != ret_query)
        {
            cout << " query failed, ret_val = " << ret_query << endl;
        }
        else
        {
            // 獲取表的列總數
            ret_val = psqlite_db->get_table_column_count(total_count_row, str_err);
            cout << "ret_val = " << ret_val << ",     total_row_count = " << total_count_row << "\n\n\n";

            std::string str_value;
            std::string str_name;
            std::string str_mark;
            std::string str_id;
            
            // 查詢表內容
            cout << "\n\n\n first time to query:\n";
            while (psqlite_db->exec_query_step())
            {
                ret_query = psqlite_db->get_column_string(0, str_id,    str_err);
                ret_query = psqlite_db->get_column_string(1, str_name,    str_err);
                ret_query = psqlite_db->get_column_string(2, str_value, str_err);
                ret_query = psqlite_db->get_column_string(3, str_mark,    str_err);


                cout << "id = " << str_id.c_str() << ",        ";
                cout << "name = " << str_name.c_str() << ",        ";
                cout << "value = " << str_value.c_str() << ",    ";
                cout << "mark = " << str_mark.c_str() << endl;
            }

            // 重置以待再次查詢該表 
            psqlite_db->reset(str_err);

            // 第二次查詢
            cout << "\n\n\n second time to query:\n";
            bool ret_val2 = psqlite_db->exec_query_step();
            while (ret_val2)
            {
                ret_query = psqlite_db->get_column_string(0, str_id, str_err);
                ret_query = psqlite_db->get_column_string(1, str_name, str_err);
                ret_query = psqlite_db->get_column_string(2, str_value, str_err);
                ret_query = psqlite_db->get_column_string(3, str_mark, str_err);


                cout << "id = " << str_id.c_str() << ",        ";
                cout << "name = " << str_name.c_str() << ",        ";
                cout << "value = " << str_value.c_str() << ",    ";
                cout << "mark = " << str_mark.c_str() << endl;

                ret_val2 = psqlite_db->exec_query_step();
            }
        }
    }

  psqlite_db->uninit();

  C、 程序執行結果:

    

6、第一版release

  編譯環境及版本:

   字符集: Unicode 字符集

the dynamic library bases on the following environment:

1. Visual Studio
    A. Microsoft Visual Studio Enterprise 2015
    B. version- 14.0.25431.01 Update 3
    C. Microsoft .NET Framework
    D. version-4.8.03752

2.OS: 
    A. Microsoft Windows 10 Professional
    B. version-    10.0.18363 version 18363
    
3. slqite version
    sqlite-amalgamation-3320300

4. Note
    Please select the compiler, which supports c++11 or upper

 

  符合上面的條件,直接下載使用吧。下載地址:https://github.com/mohistH/SQLiteCpp/releases/tag/3.0.1 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM