sqlitepp
C++ binding for the SQLite3 library
sqlitepp -- C++ wrapper for SQLite3

sqlitepp is a C++ wrapper for the official SQLite3 C API.

Compiling sqlitepp

sqlitepp uses GNU Make as a build tool. To build sqlitepp from source, download the source from git.ireas.org and then run make. You might have to change the settings in config.mk.

$ git clone https://git.ireas.org/sqlitepp
$ cd sqlitepp
$ make

Using sqlitepp

Connecting to a database

To connect to a SQLite database, you just have to create a new sqlitepp::Database object.

sqlitepp::Database database("/path/to/database.sqlite");

This snippet is equivalent to:

database.open("/path/to/database.sqlite");

If the database file does not already exist, it is created. If an error occurs during the creation of the database, a sqlitepp::DatabaseError is thrown.

Executing a simple statement

To execute a simple statement, use sqlitepp::Database::execute:

sqlitepp::Database database("/path/to/database.sqlite");
database.execute("CREATE TABLE test (id, value);");

Executing complex statements

If you want to execute more complex statements, for example selection or insertion, use prepared statements. You can prepare a statement using sqlitepp::Database::prepare. You can then bind values (if necessary) using the bind methods of sqlitepp::Statement and execute the statement using sqlitepp::Statement::execute. execute returns a sqlitepp::ResultSet that stores the returned values (if any).

Example 1: insert

The recommended way to handle insertions are named bindings:

sqlitepp::Database database("/path/to/database.sqlite");
std::shared_ptr<sqlitepp::Statement> statement = database.prepare(
"INSERT INTO test (id, value) VALUES (:id, :value);");
// insert (1, "test value")
statement->bind(":id", 1);
statement->bind(":value", "test value");
statement->execute();
statement->reset();
// insert (2, "other value")
statement->bind(":id", 2);
statement->bind(":value", "other value");
statement->execute();

Example 2: select

sqlitepp::Database database("/path/to/database.sqlite");
std::shared_ptr<sqlitepp::Statement> statement = database.prepare(
"SELECT id, value FROM test;");
ResultSet resultSet = statement.execute();
while (resultSet.canRead()) {
std::cout << "ID: " << resultSet.readInt(0) << "\tvalue: "
<< resultSet.readString(1) << std::endl;
resultSet.next();
}

Concepts

Error handling

If an error occurs during an operation, an exception is thrown. All SQLite3 database errors are wrapped in sqlitepp::DatabaseError. If a method returns, it was successful (if not stated otherwise in the method documentation).

Resources

sqlitepp uses RAII. This means that the destructors of sqlitepp::Database and sqlitepp::Statement take care of freeing their resources once they are destroyed. You can force them to free their resources using the close methods.