https://github.com/vincentlaucsb/sqlite-cpp
A simple and robust SQLite wrapper for C++
https://github.com/vincentlaucsb/sqlite-cpp
cpp sqlite sqlite3
Last synced: about 2 months ago
JSON representation
A simple and robust SQLite wrapper for C++
- Host: GitHub
- URL: https://github.com/vincentlaucsb/sqlite-cpp
- Owner: vincentlaucsb
- License: mit
- Created: 2017-12-19T03:24:10.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-07-03T05:47:30.000Z (almost 7 years ago)
- Last Synced: 2025-03-24T11:21:35.893Z (2 months ago)
- Topics: cpp, sqlite, sqlite3
- Language: C
- Homepage:
- Size: 2.11 MB
- Stars: 15
- Watchers: 2
- Forks: 4
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## SQLite for C++
[](https://travis-ci.org/vincentlaucsb/sqlite-cpp) [](https://codecov.io/gh/vincentlaucsb/sqlite-cpp) [(Documentation)](https://vincentlaucsb.github.io/sqlite-cpp/)
SQLite for C++ is a wrapper around the SQLite C API which makes it easier and safer to use.
Key features of this library are:* No manual memory management needed
* Easy to use interface, inspired by the JDBC (but with less verbose names)
* Memory safe## A Small Example
Here's a small snippet demonstrating this API. Notice how it involves no manual
wrangling with pointers? That's because this library does it all behind the scenes
in a memory-safe, low overhead way using C++ features such as RAII and smart pointers.```
include
include "sqlite_cpp.h"int main() {
SQLite::Conn db("database.sqlite");
db.exec("CREATE TABLE dillydilly (Player TEXT, Touchdown int, Interception int)");auto stmt = db.prepare("INSERT INTO dillydilly VALUES (?,?,?)");
stmt.bind("Tom Brady", 28, 7);
stmt.bind("Ben Roethlisberger", 26, 14);
stmt.bind("Matthew Stafford", 25, 9);
stmt.bind("Drew Brees", 21, 7);
stmt.bind("Philip Rivers", 24, 10);
stmt.commit(); // PreparedStatements implicitly begin transactions
// Errors lead to an automatic rollbackauto results = db.query("SELECT * FROM dillydilly");
std::vector row;
while (results.next()) {
row = results.get_row();
// Do stuff with row here
}return 0;
} // Destructors for db, stmt, results, are automatically called
```
## Basics
Most basic tasks can be executed with the methods and classes listed below### Connecting to a Database
* SQLite::Conn: To connect to a database
* SQLite::Conn.exec(): To execute a query that doesn't return anything
### Preparing Statements
* SQLite::Conn::prepare(): To prepare a statement
* SQLite::Conn::PreparedStatement
* SQLite::Conn::PreparedStatement::bind: To bind values to the statement
### Querying the Database
* SQLite::Conn::query(): To prepare/execute a query
* SQLite::Conn::ResultSet
* SQLite::Conn::ResultSet::next: To advance to the next row
## Dependencies
The library itself has no dependencies aside from a C++11 capable compiler and the SQLite library. However, a few great third-party tools were used to ensure the library's correctness.### Test Suite
* [Catch](https://github.com/catchorg/Catch2) for unit-testing
* Valgrind for memory-leak checking