在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:SQLiteCpp开源软件地址:https://gitee.com/hierfer/SQLiteCpp开源软件介绍:SQLiteC++SQLiteC++ (SQLiteCpp) is a smart and easy to use C++ SQLite3 wrapper. About SQLiteC++:SQLiteC++ offers an encapsulation around the native C APIs of SQLite,with a few intuitive and well documented C++ classes. License:Copyright (c) 2012-2021 Sébastien Rombauts ([email protected]) Distributed under the MIT License (MIT) (See accompanying file LICENSE.txtor copy at http://opensource.org/licenses/MIT) Note on redistribution of SQLite source filesAs stated by the MIT License, you are welcome to reuse, modify, and redistribute the SQLiteCpp source codethe way you want it to, be it a git submodule, a subdirectory, or a selection of some source files. I would love a mention in your README, a web link to the SQLite repository, and a mention of the author,but none of those are mandatory. About SQLite underlying library:SQLite is a library that implements a serverless transactional SQL database engine.It is the most widely deployed SQL database engine in the world.All of the code and documentation in SQLite has been dedicated to the public domain by the authors.http://www.sqlite.org/about.html The goals of SQLiteC++ are:
It is designed using the Resource Acquisition Is Initialization (RAII) idiom(see http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization),and throwing exceptions in case of SQLite errors (except in destructors,where assert() are used instead).Each SQLiteC++ object must be constructed with a valid SQLite database connection,and then is always valid until destroyed. Supported platforms:Now requires a C++11 compiler. Use branch sqlitecpp-2.x for latest pre-C++11 developments. Developments and tests are done under the following OSs:
And the following IDEs/Compilers
Dependencies
Getting startedInstallationTo use this wrapper, you need to add the SQLiteC++ source files from the src/ directoryin your project code base, and compile/link against the sqlite library. The easiest way to do this is to add the wrapper as a library.The "CMakeLists.txt" file defining the static library is provided in the root directory,so you simply have to add_subdirectory(SQLiteCpp) to you main CMakeLists.txtand link to the "SQLiteCpp" wrapper library. Example for Linux: add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/thirdparty/SQLiteCpp)add_executable(main src/main.cpp)target_link_libraries(main SQLiteCpp sqlite3 pthread dl ) Thus this SQLiteCpp repository can be directly used as a Git submodule.See the SQLiteCpp_Example side repository for a standalone "from scratch" example. Under Debian/Ubuntu/Mint Linux, you can install the libsqlite3-dev package if you don't want to use the embedded sqlite3 library. Building example and unit-tests:Use git to clone the repository. Then init and update submodule "googletest". git clone https://github.com/SRombauts/SQLiteCpp.gitcd SQLiteCppgit submodule initgit submodule update Using SQLiteCpp on a system-wide installationIf you installed this package to your system, a Here's an example of using this in your CMakeLists.txt # You can optionally define a minimum version in this callfind_package(SQLiteCpp REQUIRED)# For this example, lets say you created an target with add_executable (or add_library) called "my_target"# You can optionally declare PUBLIC or PRIVATE linkage here, depending on your needs.target_link_libraries(my_target PRIVATE SQLiteCpp) CMake and testsA CMake configuration file is also provided for multi-platform support and testing. Typical generic build for MS Visual Studio under Windows (from build.bat): mkdir buildcd buildcmake .. # cmake .. -G "Visual Studio 16 2019" # for Visual Studio 2019@REM Generate a Visual Studio solution for latest version foundcmake -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON ..@REM Build default configuration (ie 'Debug')cmake --build .@REM Build and run testsctest --output-on-failure Generating the Linux Makefile, building in Debug and executing the tests (from build.sh): mkdir Debugcd Debug# Generate a Makefile for GCC (or Clang, depanding on CC/CXX envvar)cmake -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON ..# Build (ie 'make')cmake --build .# Build and run unit-tests (ie 'make test')ctest --output-on-failure CMake options
TroubleshootingUnder Linux, if you get multiple linker errors like "undefined reference to sqlite3_xxx",it's that you lack the "sqlite3" library: install the libsqlite3-dev package. If you get a single linker error "Column.cpp: undefined reference to sqlite3_column_origin_name",it's that your "sqlite3" library was not compiled withthe SQLITE_ENABLE_COLUMN_METADATA macro defined (see http://www.sqlite.org/compile.html#enable_column_metadata).You can either recompile it yourself (seek help online) or you can comment out the following line in src/Column.h: #define SQLITE_ENABLE_COLUMN_METADATA Continuous IntegrationThis project is continuously tested under Ubuntu Linux with the gcc and clang compilersusing the Travis CI community service with the above CMake building and testing procedure.It is also tested in the same way under Windows Server 2012 R2 with Visual Studio 2013 compilerusing the AppVeyor continuous integration service. Detailed results can be seen online: Thread-safetySQLite supports three modes of thread safety, as describe in "SQLite And Multiple Threads":see http://www.sqlite.org/threadsafe.html This SQLiteC++ wrapper does no add any locks (no mutexes) nor any other thread-safety mechanismabove the SQLite library itself, by design, for lightness and speed. Thus, SQLiteC++ naturally supports the "Multi Thread" mode of SQLite:"In this mode, SQLite can be safely used by multiple threadsprovided that no single database connection is used simultaneously in two or more threads." But SQLiteC++ does not support the fully thread-safe "Serialized" mode of SQLite,because of the way it shares the underlying SQLite precompiled statementin a custom shared pointer (See the inner class "Statement::Ptr"). ExamplesThe first sample demonstrates how to query a database and get results:try{ // Open a database file SQLite::Database db("example.db3"); // Compile a SQL query, containing one parameter (index 1) SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?"); // Bind the integer value 6 to the first parameter of the SQL query query.bind(1, 6); // Loop to execute the query step by step, to get rows of result while (query.executeStep()) { // Demonstrate how to get some typed column value int id = query.getColumn(0); const char* value = query.getColumn(1); int size = query.getColumn(2); std::cout << "row: " << id << ", " << value << ", " << size << std::endl; }}catch (std::exception& e){ std::cout << "exception: " << e.what() << std::endl;} The second sample shows how to manage a transaction:try{ SQLite::Database db("transaction.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); db.exec("DROP TABLE IF EXISTS test"); // Begin transaction SQLite::Transaction transaction(db); db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; // Commit transaction transaction.commit();}catch (std::exception& e){ std::cout << "exception: " << e.what() << std::endl;} How to handle assertion in SQLiteC++:Exceptions shall not be used in destructors, so SQLiteC++ uses SQLITECPP_ASSERT() to check for errors in destructors.If you don't want assert() to be called, you have to enable and define an assert handler as shown below,and by setting the flag SQLITECPP_ENABLE_ASSERT_HANDLER when compiling the lib. #ifdef SQLITECPP_ENABLE_ASSERT_HANDLERnamespace SQLite{/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt)void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg){ // Print a message to the standard error output stream, and abort the program. std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n"; std::abort();}}#endif How to contributeGitHub websiteThe most efficient way to help and contribute to this wrapper project is touse the tools provided by GitHub:
ContactYou can also email me directly, I will try to answer questions and requests whenever I get the time for it. Coding Style GuidelinesThe source code use the CamelCase naming style variant where:
See also - Some other simple C++ SQLite wrappers:See bellow a short comparison of other wrappers done at the time of writing:
|
请发表评论