基于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