sqlitepp
C++ binding for the SQLite3 library
Public Member Functions | List of all members
sqlitepp::Database Class Reference

A handle for a SQLite3 database. More...

#include <sqlitepp/sqlitepp.h>

Inheritance diagram for sqlitepp::Database:
Inheritance graph
[legend]
Collaboration diagram for sqlitepp::Database:
Collaboration graph
[legend]

Public Member Functions

 Database ()
 Creates a new closed database. More...
 
 Database (const std::string &file)
 Creates a new database and opens the given file. More...
 
 ~Database ()
 Destructs this object and closes the database connection. More...
 
void close ()
 Closes the database if it is open. More...
 
void execute (const std::string &sql)
 Executes the given SQL string. More...
 
int lastInsertRowId () const
 Returns the row ID of the last element that was inserted. More...
 
void open (const std::string &file)
 Opens the given database file. More...
 
std::shared_ptr< Statementprepare (const std::string &sql)
 Prepares a statement and returns a pointer to it. More...
 
- Public Member Functions inherited from sqlitepp::Openable
bool isOpen () const
 Checks whether this object is open. More...
 

Additional Inherited Members

- Protected Member Functions inherited from sqlitepp::Openable
 Openable (const bool open, const std::string &name)
 Creates a new Openable. More...
 
void requireOpen () const
 Requires this object to be open and throws an exception if it is not. More...
 
void setOpen (const bool open)
 Changes the state of this object. More...
 

Detailed Description

A handle for a SQLite3 database.

This class stores a reference to a SQLite3 database and provides methods to open, query and change this database. After you successfully opened a database (using the constructor Database(const std::string&) or using the open(const std::string&) method), you can execute and prepare statements. You can check whether the database is currently open using isOpen().

If you try to call a method that queries or updates the database and the database is not open, a std::logic_error is thrown. If a database operation fails, a DatabaseError is thrown.

Constructor & Destructor Documentation

◆ Database() [1/2]

sqlitepp::Database::Database ( )

Creates a new closed database.

Before you can access this database, you have to open a database file using open(std::string&).

◆ Database() [2/2]

sqlitepp::Database::Database ( const std::string &  file)
explicit

Creates a new database and opens the given file.

The given file must either be a valid SQLite3 database file or may not exist yet. This constructor is an abbreviation for:

Database database;
database.open(file);
Parameters
filethe name of the database file (not required to exist)
Exceptions
std::runtime_errorif there is not enough memory to create a database connection
DatabaseErrorif the SQLite3 database could not be opened

◆ ~Database()

sqlitepp::Database::~Database ( )

Destructs this object and closes the database connection.

Errors that occur closing the database are ignored.

Member Function Documentation

◆ close()

void sqlitepp::Database::close ( )

Closes the database if it is open.

Exceptions
DatabaseErrorif the database cannot be closed

◆ execute()

void sqlitepp::Database::execute ( const std::string &  sql)

Executes the given SQL string.

You can only call this method if there is an open database connection. If you want to access the values returned by a SQL statement, use prepare(const std::string&) instead.

Parameters
sqlthe SQL statement to execute
Exceptions
std::logic_errorif the database is not open
DatabaseErrorif an error occurred during the execution

◆ lastInsertRowId()

int sqlitepp::Database::lastInsertRowId ( ) const

Returns the row ID of the last element that was inserted.

If no entry has been inserted into the database, this method returns zero.

Returns
the index of the last element inserted into the database or zero
Exceptions
std::logic_errorif the database is not open
DatabaseErrorif an error occurred during the execution

◆ open()

void sqlitepp::Database::open ( const std::string &  file)

Opens the given database file.

The given file must either be a valid SQLite3 database file or may not exist yet. You can only open a new connection when the previous connection has been closed (if any).

Parameters
filethe name of the database file (not required to exist)
Exceptions
std::logic_errorif the database is already open
std::runtime_errorif there is not enough memory to create a database connection
DatabaseErrorif the SQLite3 database could not be opened

◆ prepare()

std::shared_ptr< Statement > sqlitepp::Database::prepare ( const std::string &  sql)

Prepares a statement and returns a pointer to it.

You can either pass a complete SQL statement or a statement with wildcards. If you use wildcards, you can bind them to a value using the returned Statement.

Parameters
sqlthe SQL statement to prepare (may contain wildcards)
Returns
a pointer to the prepared statement
Exceptions
std::logic_errorif the database is not open
DatabaseErrorif an error occurred during the preparation

The documentation for this class was generated from the following files: