|
| 1 | +SQLiteC++ |
| 2 | +--------- |
| 3 | + |
| 4 | +[](https://github.com/SRombauts/SQLiteCpp/releases) |
| 5 | +[](https://github.com/SRombauts/SQLiteCpp/blob/master/LICENSE.txt) |
| 6 | +[](https://travis-ci.org/SRombauts/SQLiteCpp "Travis CI Linux Build Status") |
| 7 | +[](https://ci.appveyor.com/project/SbastienRombauts/SQLiteCpp "AppVeyor Windows Build status") |
| 8 | +[](https://github.com/SRombauts/SQLiteCpp/actions "GitHhub Actions Build status") |
| 9 | +[](https://coveralls.io/github/SRombauts/SQLiteCpp "Coveralls test coverage") |
| 10 | +[](https://scan.coverity.com/projects/srombauts-sqlitecpp "Coverity Scan Build Status") |
| 11 | +[](https://gitter.im/SRombauts/SQLiteCpp?utm_source=badge&utm_medium=badge&utm_campaign=pr-badge&utm_content=badge) |
| 12 | + |
| 13 | +SQLiteC++ (SQLiteCpp) is a smart and easy to use C++ SQLite3 wrapper. |
| 14 | + |
| 15 | +Keywords: sqlite, sqlite3, C, library, wrapper C++ |
| 16 | + |
| 17 | +## About SQLiteC++: |
| 18 | + |
| 19 | +SQLiteC++ offers an encapsulation around the native C APIs of SQLite, |
| 20 | +with a few intuitive and well documented C++ classes. |
| 21 | + |
| 22 | +### License: |
| 23 | + |
| 24 | +Copyright (c) 2012-2020 Sébastien Rombauts ( [email protected]) |
| 25 | +<a href="https://www.paypal.me/SRombauts" title="Pay Me a Beer! Donate with PayPal :)"><img src="https://www.paypalobjects.com/webstatic/paypalme/images/pp_logo_small.png" width="118"></a> |
| 26 | + |
| 27 | +Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt |
| 28 | +or copy at http://opensource.org/licenses/MIT) |
| 29 | + |
| 30 | +#### Note on redistribution of SQLite source files |
| 31 | + |
| 32 | +As stated by the MIT License, you are welcome to reuse, modify, and redistribute the SQLiteCpp source code |
| 33 | +the way you want it to, be it a git submodule, a subdirectory, or a selection of some source files. |
| 34 | + |
| 35 | +I would love a mention in your README, a web link to the SQLite repository, and a mention of the author, |
| 36 | +but none of those are mandatory. |
| 37 | + |
| 38 | +### About SQLite underlying library: |
| 39 | + |
| 40 | +SQLite is a library that implements a serverless transactional SQL database engine. |
| 41 | +It is the most widely deployed SQL database engine in the world. |
| 42 | +All of the code and documentation in SQLite has been dedicated to the public domain by the authors. |
| 43 | +http://www.sqlite.org/about.html |
| 44 | + |
| 45 | +### The goals of SQLiteC++ are: |
| 46 | + |
| 47 | +- to offer the best of the existing simple C++ SQLite wrappers |
| 48 | +- to be elegantly written with good C++11 design, STL, exceptions and RAII idiom |
| 49 | +- to keep dependencies to a minimum (C++11 STL and SQLite3) |
| 50 | +- to be portable |
| 51 | +- to be light and fast |
| 52 | +- to be thread-safe only as much as SQLite "Multi-thread" mode (see below) |
| 53 | +- to have a good unit test coverage |
| 54 | +- to use API names sticking with those of the SQLite library |
| 55 | +- to be well documented with Doxygen tags, and with some good examples |
| 56 | +- to be well maintained |
| 57 | +- to use a permissive MIT license, similar to BSD or Boost, for proprietary/commercial usage |
| 58 | + |
| 59 | +It is designed using the Resource Acquisition Is Initialization (RAII) idiom |
| 60 | +(see http://en.wikipedia.org/wiki/Resource_Acquisition_Is_Initialization), |
| 61 | +and throwing exceptions in case of SQLite errors (except in destructors, |
| 62 | +where assert() are used instead). |
| 63 | +Each SQLiteC++ object must be constructed with a valid SQLite database connection, |
| 64 | +and then is always valid until destroyed. |
| 65 | + |
| 66 | +### Supported platforms: |
| 67 | + |
| 68 | +Now requires a C++11 compiler. Use branch [sqlitecpp-2.x](https://github.com/SRombauts/SQLiteCpp/tree/sqlitecpp-2.x) for latest pre-C++11 developments. |
| 69 | + |
| 70 | +Developments and tests are done under the following OSs: |
| 71 | +- Ubuntu 14.04, 16.04 and 18.04 (Travis CI) |
| 72 | +- Windows 10, and Windows Server 2012 R2 & Windows Server 2016 (AppVeyor) |
| 73 | +- OS X 10.11 (Travis CI) |
| 74 | +- Github Actions |
| 75 | +- Valgrind memcheck tool |
| 76 | + |
| 77 | +And the following IDEs/Compilers |
| 78 | +- GCC 4.8.4, 5.3.0 and 7.1.1 (C++11, C++14, C++17) |
| 79 | +- Clang 5 |
| 80 | +- Xcode 8 & 9 |
| 81 | +- Visual Studio Community 2019, 2017, and 2015 (AppVeyor) |
| 82 | + |
| 83 | +### Dependencies |
| 84 | + |
| 85 | +- a modern C++11 STL implementation with GCC, Clang, or Visual Studio 2015 |
| 86 | +- exception support (the class Exception inherits from std::runtime_error) |
| 87 | +- the SQLite library (3.7.15 minimum from 2012-12-12) either by linking to it dynamically or statically (install the libsqlite3-dev package under Debian/Ubuntu/Mint Linux), |
| 88 | + or by adding its source file in your project code base (source code provided in src/sqlite3 for Windows), |
| 89 | + with the SQLITE_ENABLE_COLUMN_METADATA macro defined (see http://www.sqlite.org/compile.html#enable_column_metadata). |
| 90 | + |
| 91 | +## Getting started |
| 92 | +### Installation |
| 93 | + |
| 94 | +To use this wrapper, you need to add the SQLiteC++ source files from the src/ directory |
| 95 | +in your project code base, and compile/link against the sqlite library. |
| 96 | + |
| 97 | +The easiest way to do this is to add the wrapper as a library. |
| 98 | +The "CMakeLists.txt" file defining the static library is provided in the root directory, |
| 99 | +so you simply have to add_subdirectory(SQLiteCpp) to you main CMakeLists.txt |
| 100 | +and link to the "SQLiteCpp" wrapper library. |
| 101 | + |
| 102 | +Example for Linux: |
| 103 | +```cmake |
| 104 | +add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/thirdparty/SQLiteCpp) |
| 105 | +
|
| 106 | +include_directories( |
| 107 | + ${CMAKE_CURRENT_LIST_DIR}/thirdparty/SQLiteCpp/include |
| 108 | +) |
| 109 | +
|
| 110 | +add_executable(main src/main.cpp) |
| 111 | +target_link_libraries(main |
| 112 | + SQLiteCpp |
| 113 | + sqlite3 |
| 114 | + pthread |
| 115 | + dl |
| 116 | + ) |
| 117 | +``` |
| 118 | +Thus this SQLiteCpp repository can be directly used as a Git submodule. |
| 119 | +See the [SQLiteCpp_Example](https://github.com/SRombauts/SQLiteCpp_Example) side repository for a standalone "from scratch" example. |
| 120 | + |
| 121 | +Under Debian/Ubuntu/Mint Linux, you can install the libsqlite3-dev package if you don't want to use the embedded sqlite3 library. |
| 122 | + |
| 123 | +### Building example and unit-tests: |
| 124 | + |
| 125 | +Use git to clone the repository. Then init and update submodule "googletest". |
| 126 | + |
| 127 | +```Shell |
| 128 | +git clone https://github.com/SRombauts/SQLiteCpp.git |
| 129 | +cd SQLiteCpp |
| 130 | +git submodule init |
| 131 | +git submodule update |
| 132 | +``` |
| 133 | + |
| 134 | +#### CMake and tests |
| 135 | +A CMake configuration file is also provided for multi-platform support and testing. |
| 136 | + |
| 137 | +Typical generic build for MS Visual Studio under Windows (from [build.bat](build.bat)): |
| 138 | + |
| 139 | +```Batchfile |
| 140 | +mkdir build |
| 141 | +cd build |
| 142 | +
|
| 143 | +cmake .. # cmake .. -G "Visual Studio 16 2019" # for Visual Studio 2019 |
| 144 | +@REM Generate a Visual Studio solution for latest version found |
| 145 | +cmake -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON .. |
| 146 | +
|
| 147 | +@REM Build default configuration (ie 'Debug') |
| 148 | +cmake --build . |
| 149 | +
|
| 150 | +@REM Build and run tests |
| 151 | +ctest --output-on-failure |
| 152 | +``` |
| 153 | + |
| 154 | +Generating the Linux Makefile, building in Debug and executing the tests (from [build.sh](build.sh)): |
| 155 | + |
| 156 | +```Shell |
| 157 | +mkdir Debug |
| 158 | +cd Debug |
| 159 | + |
| 160 | +# Generate a Makefile for GCC (or Clang, depanding on CC/CXX envvar) |
| 161 | +cmake -DSQLITECPP_BUILD_EXAMPLES=ON -DSQLITECPP_BUILD_TESTS=ON .. |
| 162 | + |
| 163 | +# Build (ie 'make') |
| 164 | +cmake --build . |
| 165 | + |
| 166 | +# Build and run unit-tests (ie 'make test') |
| 167 | +ctest --output-on-failure |
| 168 | +``` |
| 169 | + |
| 170 | +#### CMake options |
| 171 | + |
| 172 | + * For more options on customizing the build, see the [CMakeLists.txt](https://github.com/SRombauts/SQLiteCpp/blob/master/CMakeLists.txt) file. |
| 173 | + |
| 174 | +#### Troubleshooting |
| 175 | + |
| 176 | +Under Linux, if you get multiple linker errors like "undefined reference to sqlite3_xxx", |
| 177 | +it's that you lack the "sqlite3" library: install the libsqlite3-dev package. |
| 178 | + |
| 179 | +If you get a single linker error "Column.cpp: undefined reference to sqlite3_column_origin_name", |
| 180 | +it's that your "sqlite3" library was not compiled with |
| 181 | +the SQLITE_ENABLE_COLUMN_METADATA macro defined (see http://www.sqlite.org/compile.html#enable_column_metadata). |
| 182 | +You can either recompile it yourself (seek help online) or you can comment out the following line in src/Column.h: |
| 183 | + |
| 184 | +```C++ |
| 185 | +#define SQLITE_ENABLE_COLUMN_METADATA |
| 186 | +``` |
| 187 | + |
| 188 | +### Continuous Integration |
| 189 | + |
| 190 | +This project is continuously tested under Ubuntu Linux with the gcc and clang compilers |
| 191 | +using the Travis CI community service with the above CMake building and testing procedure. |
| 192 | +It is also tested in the same way under Windows Server 2012 R2 with Visual Studio 2013 compiler |
| 193 | +using the AppVeyor continuous integration service. |
| 194 | + |
| 195 | +Detailed results can be seen online: |
| 196 | + - https://travis-ci.org/SRombauts/SQLiteCpp |
| 197 | + - https://ci.appveyor.com/project/SbastienRombauts/SQLiteCpp |
| 198 | + |
| 199 | +### Thread-safety |
| 200 | + |
| 201 | +SQLite supports three modes of thread safety, as describe in "SQLite And Multiple Threads": |
| 202 | +see http://www.sqlite.org/threadsafe.html |
| 203 | + |
| 204 | +This SQLiteC++ wrapper does no add any locks (no mutexes) nor any other thread-safety mechanism |
| 205 | +above the SQLite library itself, by design, for lightness and speed. |
| 206 | + |
| 207 | +Thus, SQLiteC++ naturally supports the "Multi Thread" mode of SQLite: |
| 208 | +"In this mode, SQLite can be safely used by multiple threads |
| 209 | +provided that no single database connection is used simultaneously in two or more threads." |
| 210 | + |
| 211 | +But SQLiteC++ does not support the fully thread-safe "Serialized" mode of SQLite, |
| 212 | +because of the way it shares the underlying SQLite precompiled statement |
| 213 | +in a custom shared pointer (See the inner class "Statement::Ptr"). |
| 214 | + |
| 215 | +## Examples |
| 216 | +### The first sample demonstrates how to query a database and get results: |
| 217 | + |
| 218 | +```C++ |
| 219 | +try |
| 220 | +{ |
| 221 | + // Open a database file |
| 222 | + SQLite::Database db("example.db3"); |
| 223 | + |
| 224 | + // Compile a SQL query, containing one parameter (index 1) |
| 225 | + SQLite::Statement query(db, "SELECT * FROM test WHERE size > ?"); |
| 226 | + |
| 227 | + // Bind the integer value 6 to the first parameter of the SQL query |
| 228 | + query.bind(1, 6); |
| 229 | + |
| 230 | + // Loop to execute the query step by step, to get rows of result |
| 231 | + while (query.executeStep()) |
| 232 | + { |
| 233 | + // Demonstrate how to get some typed column value |
| 234 | + int id = query.getColumn(0); |
| 235 | + const char* value = query.getColumn(1); |
| 236 | + int size = query.getColumn(2); |
| 237 | + |
| 238 | + std::cout << "row: " << id << ", " << value << ", " << size << std::endl; |
| 239 | + } |
| 240 | +} |
| 241 | +catch (std::exception& e) |
| 242 | +{ |
| 243 | + std::cout << "exception: " << e.what() << std::endl; |
| 244 | +} |
| 245 | +``` |
| 246 | + |
| 247 | +### The second sample shows how to manage a transaction: |
| 248 | + |
| 249 | +```C++ |
| 250 | +try |
| 251 | +{ |
| 252 | + SQLite::Database db("transaction.db3", SQLite::OPEN_READWRITE|SQLite::OPEN_CREATE); |
| 253 | + |
| 254 | + db.exec("DROP TABLE IF EXISTS test"); |
| 255 | + |
| 256 | + // Begin transaction |
| 257 | + SQLite::Transaction transaction(db); |
| 258 | + |
| 259 | + db.exec("CREATE TABLE test (id INTEGER PRIMARY KEY, value TEXT)"); |
| 260 | + |
| 261 | + int nb = db.exec("INSERT INTO test VALUES (NULL, \"test\")"); |
| 262 | + std::cout << "INSERT INTO test VALUES (NULL, \"test\")\", returned " << nb << std::endl; |
| 263 | + |
| 264 | + // Commit transaction |
| 265 | + transaction.commit(); |
| 266 | +} |
| 267 | +catch (std::exception& e) |
| 268 | +{ |
| 269 | + std::cout << "exception: " << e.what() << std::endl; |
| 270 | +} |
| 271 | +``` |
| 272 | + |
| 273 | +### How to handle assertion in SQLiteC++: |
| 274 | +Exceptions shall not be used in destructors, so SQLiteC++ uses SQLITECPP_ASSERT() to check for errors in destructors. |
| 275 | +If you don't want assert() to be called, you have to enable and define an assert handler as shown below, |
| 276 | +and by setting the flag SQLITECPP_ENABLE_ASSERT_HANDLER when compiling the lib. |
| 277 | + |
| 278 | +```C++ |
| 279 | +#ifdef SQLITECPP_ENABLE_ASSERT_HANDLER |
| 280 | +namespace SQLite |
| 281 | +{ |
| 282 | +/// definition of the assertion handler enabled when SQLITECPP_ENABLE_ASSERT_HANDLER is defined in the project (CMakeList.txt) |
| 283 | +void assertion_failed(const char* apFile, const long apLine, const char* apFunc, const char* apExpr, const char* apMsg) |
| 284 | +{ |
| 285 | + // Print a message to the standard error output stream, and abort the program. |
| 286 | + std::cerr << apFile << ":" << apLine << ":" << " error: assertion failed (" << apExpr << ") in " << apFunc << "() with message \"" << apMsg << "\"\n"; |
| 287 | + std::abort(); |
| 288 | +} |
| 289 | +} |
| 290 | +#endif |
| 291 | +``` |
| 292 | + |
| 293 | +## How to contribute |
| 294 | +### GitHub website |
| 295 | +The most efficient way to help and contribute to this wrapper project is to |
| 296 | +use the tools provided by GitHub: |
| 297 | +- please fill bug reports and feature requests here: https://github.com/SRombauts/SQLiteCpp/issues |
| 298 | +- fork the repository, make some small changes and submit them with pull-request |
| 299 | + |
| 300 | +### Contact |
| 301 | +You can also email me directly, I will try to answer questions and requests whenever I get the time for it. |
| 302 | + |
| 303 | +### Coding Style Guidelines |
| 304 | +The source code use the CamelCase naming style variant where: |
| 305 | +- type names (class, struct, typedef, enums...) begin with a capital letter |
| 306 | +- files (.cpp/.h) are named like the class they contain |
| 307 | +- function and variable names begin with a lower case letter |
| 308 | +- member variables begin with a 'm', function arguments begin with a 'a', booleans with a 'b', pointers with a 'p' |
| 309 | +- each file, class, method and member variable is documented using Doxygen tags |
| 310 | +- braces on their own line |
| 311 | +See also http://www.appinf.com/download/CppCodingStyleGuide.pdf for good guidelines |
| 312 | + |
| 313 | +## See also - Some other simple C++ SQLite wrappers: |
| 314 | + |
| 315 | +See bellow a short comparison of other wrappers done at the time of writing: |
| 316 | + - [sqdbcpp](http://code.google.com/p/sqdbcpp/): RAII design, simple, no dependencies, UTF-8/UTF-16, new BSD license |
| 317 | + - [sqlite3cc](http://ed.am/dev/sqlite3cc): uses boost, modern design, LPGPL |
| 318 | + - [sqlite3pp](https://github.com/iwongu/sqlite3pp): modern design inspired by boost, MIT License |
| 319 | + - [SQLite++](http://sqlitepp.berlios.de/): uses boost build system, Boost License 1.0 |
| 320 | + - [CppSQLite](http://www.codeproject.com/Articles/6343/CppSQLite-C-Wrapper-for-SQLite/): famous Code Project but old design, BSD License |
| 321 | + - [easySQLite](http://code.google.com/p/easysqlite/): manages table as structured objects, complex |
| 322 | + - [sqlite_modern_cpp](https://github.com/keramer/sqlite_modern_cpp): modern C++11, all in one file, MIT license |
| 323 | + - [sqlite_orm](https://github.com/fnc12/sqlite_orm): modern C++14, header only all in one file, no raw string queries, BSD-3 license |
0 commit comments