dbc-cpp is a lightweight C++ database access library loosely following the
JDBC API. It currently supports the SQLite backend. If needed, other
backends can be easily supported as well.
It has the following features:
- the database connection,
- prepared statements for binding parameters to and executing queries and DML/DDL statements,
- result sets for accessing rows and columns from query results.
Development status: testing/beta.
dbc-cpp is licenced under the MIT licence.
Be sure to pull in submodules with git clone --recursive (see below
under Building).
Build and test dbc-cpp as follows (clang++ is the default compiler):
git clone --recursive git://github.com/mrts/dbc-cpp.git cd dbc-cpp make -j 4 make test
Includes are in include and the library will be in lib.
Add -I$(DBCCPPDIR)/include to include path and
-L$(DBCCPPDIR)/lib -ldbccpp -lsqlite3 to linker flags in your
project's Makefile.
Quick overview:
#include <dbccpp/dbccpp.h>
// call connect() with driver and file name to create the connection instance
dbc::DbConnection::connect("sqlite", "test.db");
// call instance() to access the connection instance
dbc::DbConnection& db = dbc::DbConnection::instance();
// execute DDL statements directly with the connection object
db.executeUpdate("CREATE TABLE IF NOT EXISTS person "
"(id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT NOT NULL)");
// use prepared statements and argument binding for DML statements
dbc::PreparedStatement::ptr insert = db.prepareStatement("INSERT INTO "
"person (name) VALUES (?)");
insert << "Ervin"; // or insert->set(1, "Ervin");
// DML statements return number of updated rows
assert(insert->executeUpdate() == 1);
// use prepared statements and argument binding for queries
dbc::PreparedStatement::ptr select = db.prepareStatement("SELECT DISTINCT "
"name FROM person WHERE name LIKE ? ORDER BY name");
select << "%vin";
// queries return result sets
dbc::ResultSet::ptr results_ptr = select->executeQuery();
dbc::ResultSet& results = *results_ptr;
// use next() to fetch and iterate over results
while (results.next())
{
// get strings by copy (recommended, rely on RVO)
std::string name = results[0];
// or into an out parameter (by reference)
results.get<std::string>(0, name);
}
See main test for more details.