SQLiteC++  0.5.0
SQLiteC++ is a smart and easy to use C++ SQLite3 wrapper.
 All Classes Namespaces Files Functions Friends Macros
SQLite::Database Class Reference

RAII management of a SQLite Database Connection. More...

#include <Database.h>

Public Member Functions

 Database (const char *apFilename, const int aFlags=SQLITE_OPEN_READONLY)
 Open the provided database UTF-8 filename. More...
 
virtual ~Database (void) throw ()
 Close the SQLite database connection. More...
 
int exec (const char *apQueries)
 Shortcut to execute one or multiple statements without results. More...
 
Column execAndGet (const char *apQuery)
 Shortcut to execute a one step query and fetch the first column of the result. More...
 
bool tableExists (const char *apTableName)
 Shortcut to test if a table exists. More...
 
int setBusyTimeout (int aTimeoutMs)
 Set a busy handler that sleeps for a specified amount of time when a table is locked. More...
 
sqlite3_int64 getLastInsertRowid (void) const
 Get the rowid of the most recent successful INSERT into the database from the current connection. More...
 
const std::string & getFilename (void) const
 Return the filename used to open the database. More...
 
const char * errmsg (void) const
 Return UTF-8 encoded English language explanation of the most recent error. More...
 

Friends

class Statement
 

Detailed Description

RAII management of a SQLite Database Connection.

A Database object manage a list of all SQLite Statements associated with the underlying SQLite 3 database connection.

Resource Acquisition Is Initialization (RAII) means that the Database Connection is opened in the constructor and closed in the destructor, so that there is no need to worry about memory management or the validity of the underlying SQLite Connection.

Definition at line 29 of file Database.h.

Constructor & Destructor Documentation

SQLite::Database::Database ( const char *  apFilename,
const int  aFlags = SQLITE_OPEN_READONLY 
)

Open the provided database UTF-8 filename.

Uses sqlite3_open_v2() with readonly default flag, which is the opposite behavior of the old sqlite3_open() function (READWRITE+CREATE). This makes sense if you want to use it on a readonly filesystem or to prevent creation of a void file when a required file is missing.

Exception is thrown in case of error, then the Database object is NOT constructed.

Parameters
[in]apFilenameUTF-8 path/uri to the database file ("filename" sqlite3 parameter)
[in]aFlagsSQLITE_OPEN_READONLY/SQLITE_OPEN_READWRITE/SQLITE_OPEN_CREATE...

Definition at line 18 of file Database.cpp.

SQLite::Database::~Database ( void  ) throw ()
virtual

Close the SQLite database connection.

All SQLite statements must have been finalized before, so all Statement objects must have been unregistered.

Definition at line 32 of file Database.cpp.

Member Function Documentation

const char* SQLite::Database::errmsg ( void  ) const
inline

Return UTF-8 encoded English language explanation of the most recent error.

Definition at line 141 of file Database.h.

int SQLite::Database::exec ( const char *  apQueries)

Shortcut to execute one or multiple statements without results.

This is useful for any kind of statements other than the Data Query Language (DQL) "SELECT" :

  • Data Definition Language (DDL) statements "CREATE", "ALTER" and "DROP"
  • Data Manipulation Language (DML) statements "INSERT", "UPDATE" and "DELETE"
  • Data Control Language (DCL) statements "GRANT", "REVOKE", "COMMIT" and "ROLLBACK"
See Also
Statement::exec() to handle precompiled statements (for better performances) without results
Statement::executeStep() to handle "SELECT" queries with results
Parameters
[in]apQueriesone or multiple UTF-8 encoded, semicolon-separate SQL statements
Returns
number of rows modified by those SQL statements (INSERT, UPDATE or DELETE)
Exceptions
SQLite::Exceptionin case of error

Definition at line 41 of file Database.cpp.

Here is the caller graph for this function:

Column SQLite::Database::execAndGet ( const char *  apQuery)

Shortcut to execute a one step query and fetch the first column of the result.

This is a shortcut to execute a simple statement with a single result. This should be used only for non reusable queries (else you should use a Statement with bind()). This should be used only for queries with expected results (else an exception is fired).

Warning
WARNING: Be very careful with this dangerous method: you have to make a COPY OF THE result, else it will be destroy before the next line (when the underlying temporary Statement and Column objects are destroyed)
See Also
also Statement class for handling queries with multiple results
Parameters
[in]apQueryan UTF-8 encoded SQL query
Returns
a temporary Column object with the first column of result.
Exceptions
SQLite::Exceptionin case of error

Definition at line 56 of file Database.cpp.

Here is the call graph for this function:

const std::string& SQLite::Database::getFilename ( void  ) const
inline

Return the filename used to open the database.

Definition at line 133 of file Database.h.

sqlite3_int64 SQLite::Database::getLastInsertRowid ( void  ) const
inline

Get the rowid of the most recent successful INSERT into the database from the current connection.

Returns
Rowid of the most recent successful INSERT into the database, or 0 if there was none.

Definition at line 125 of file Database.h.

int SQLite::Database::setBusyTimeout ( int  aTimeoutMs)
inline

Set a busy handler that sleeps for a specified amount of time when a table is locked.

Parameters
[in]aTimeoutMsAmount of milliseconds to wait before returning SQLITE_BUSY

Definition at line 115 of file Database.h.

bool SQLite::Database::tableExists ( const char *  apTableName)

Shortcut to test if a table exists.

Table names are case sensitive.

Parameters
[in]apTableNamean UTF-8 encoded case sensitive Table name
Returns
true if the table exists.
Exceptions
SQLite::Exceptionin case of error

Definition at line 64 of file Database.cpp.

Here is the call graph for this function:

Friends And Related Function Documentation

friend class Statement
friend

Definition at line 31 of file Database.h.


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