An open API service indexing awesome lists of open source software.

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++

Awesome Lists containing this project

README

        

## SQLite for C++

[![Build Status](https://travis-ci.org/vincentlaucsb/sqlite-cpp.svg?branch=master)](https://travis-ci.org/vincentlaucsb/sqlite-cpp) [![codecov](https://codecov.io/gh/vincentlaucsb/sqlite-cpp/branch/master/graph/badge.svg)](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 rollback

auto 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