Skip to content
This repository was archived by the owner on May 6, 2026. It is now read-only.

Database

Nicuvëo edited this page Mar 29, 2014 · 5 revisions

Overview

A Database object is a wrapper around a raw sqlite3* pointer. The pointer is stored in a boost::shared_ptr, making Databases instances flyweight and copyable. When the last Database object owning a given sqlite3* pointer is deleted, the connection is closed.

--

Constructors

Name Description
Database() Constructs an empty database object.
Database(const char* filename) Constructs a new database / opens an existing database.

--

Accessors

Name Description
bool connected() const Answers whether the given instance is connected to a real database.
Statement make(const char* query) const Prepares a statement from the given query.
sqlite3* raw_data() const Access the underlying raw sqlite3* pointer.

Notes:

  • db.connected() is equivalent to db.raw_data() != 0.
  • The raw_data pointer is not meant to be stored and used outside of the Database instance, as its lifetime is managed via reference counting in the instance itself.

--

Mutators

Name Description
void connect(const char* filename) Constructs a new database / opens an existing database.
void disconnect() Disconnects the instance from its current database if any.

--

Execution

Name Description
Rows exec(const char* q) const Execute a query -> rows
Rows exec(const char* q, const Parameters& ps) const Execute a query -> rows
void exec(const char* q, const RowCallback& cb) const Execute a query, callback on each row.
void exec(const char* q, const RowCallback& cb, const Parameters& ps) const Execute a query, callback on each row.
RowRange result(const char* q) const Execute a query -> row range.
RowRange result(const char* q, const Parameters& ps) const Execute a query -> row range.

Notes:

  • Rows store the whole query result in memory, while RowRange allows you to iterate on the results one database row at a time.
  • Use the provided saw_foreach macro to iterate on a RowRange:
saw_foreach (const Row& r, db.result("select * from clients"))
{
  // do something with r
}

Clone this wiki locally