sqlitepp
C++ binding for the SQLite3 library
|
A handle for a SQLite3 database. More...
#include <sqlitepp/sqlitepp.h>
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< Statement > | prepare (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... | |
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.
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&).
|
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:
file | the name of the database file (not required to exist) |
std::runtime_error | if there is not enough memory to create a database connection |
DatabaseError | if the SQLite3 database could not be opened |
sqlitepp::Database::~Database | ( | ) |
Destructs this object and closes the database connection.
Errors that occur closing the database are ignored.
void sqlitepp::Database::close | ( | ) |
Closes the database if it is open.
DatabaseError | if the database cannot be closed |
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.
sql | the SQL statement to execute |
std::logic_error | if the database is not open |
DatabaseError | if an error occurred during the execution |
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.
std::logic_error | if the database is not open |
DatabaseError | if an error occurred during the execution |
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).
file | the name of the database file (not required to exist) |
std::logic_error | if the database is already open |
std::runtime_error | if there is not enough memory to create a database connection |
DatabaseError | if the SQLite3 database could not be opened |
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.
sql | the SQL statement to prepare (may contain wildcards) |
std::logic_error | if the database is not open |
DatabaseError | if an error occurred during the preparation |