SQLiteCpp/src/SQLiteC++/Database.cpp
2012-04-16 14:59:51 +02:00

63 lines
1.7 KiB
C++

/**
* @file Database.cpp
* @brief Management of a SQLite Database Connection.
*
* Copyright (c) 2012 Sebastien Rombauts (sebastien.rombauts@gmail.com)
*
* Distributed under the MIT License (MIT) (See accompanying file LICENSE.txt
* or copy at http://opensource.org/licenses/MIT)
*/
#include "Database.h"
#include "Statement.h"
#include "Column.h"
namespace SQLite
{
// Open the provided database UTF-8 filename.
Database::Database(const char* apFilename, const int aFlags /*= SQLITE_OPEN_READONLY*/) : // throw(SQLite::Exception)
mpSQLite(NULL),
mFilename(apFilename)
{
int ret = sqlite3_open_v2(apFilename, &mpSQLite, aFlags, NULL);
if (SQLITE_OK != ret)
{
std::string strerr = sqlite3_errmsg(mpSQLite);
sqlite3_close(mpSQLite);
throw SQLite::Exception(strerr);
}
}
// Close the SQLite database connection.
Database::~Database(void) throw() // nothrow
{
int ret = sqlite3_close(mpSQLite);
if (SQLITE_OK != ret)
{
// Never throw an exception in a destructor
//std::cout << sqlite3_errmsg(mpSQLite);
}
}
// Shortcut to execute one or multiple SQL statements without results.
int Database::exec(const char* apQueries) // throw(SQLite::Exception);
{
int ret = sqlite3_exec(mpSQLite, apQueries, NULL, NULL, NULL);
check(ret);
// Return the number of changes made by those SQL statements
return sqlite3_changes(mpSQLite);
}
// Check if aRet equal SQLITE_OK, else throw a SQLite::Exception with the SQLite error message
void Database::check(const int aRet) const // throw(SQLite::Exception)
{
if (SQLITE_OK != aRet)
{
throw SQLite::Exception(sqlite3_errmsg(mpSQLite));
}
}
}; // namespace SQLite