23 SQLiteException DatabaseNotOpened(
"Database has not been opened yet.");
24 SQLiteException DatabaseOpened(
"A database has been opened already.");
26 inline void Database::checkDatabaseOpened()
const {
28 throw DatabaseNotOpened;
33 this->checkDatabaseOpened();
35 return sqlite3_last_insert_rowid(this->database);
40 this->
open(file, flags);
47 void Database::init(
void) {
48 this->isopen = this->transaction =
false;
49 this->database = NULL;
58 int flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
62 flag = SQLITE_OPEN_READONLY;
66 flag = SQLITE_OPEN_READWRITE;
70 flag = SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE;
74 this->lastResult = sqlite3_open_v2(file.c_str(), &this->database, flag, NULL);
75 if(this->lastResult != SQLITE_OK) {
83 this->
exec(
"PRAGMA foreign_keys = ON;");
87 this->
exec(
"PRAGMA foreign_keys = OFF;");
99 this->checkDatabaseOpened();
101 this->lastResult = sqlite3_exec(this->database, str.c_str(), NULL, NULL, NULL);
102 if(this->lastResult != SQLITE_OK) {
106 return sqlite3_changes(this->database);
112 if(this->transaction) {
116 std::string flag(
"DEFERRED");
130 std::string execStr =
"BEGIN " + flag +
" TRANSACTION;";
133 this->transaction =
true;
138 if(!this->transaction) {
142 this->
exec(
"ROLLBACK;");
143 this->transaction =
false;
148 if(!this->transaction) {
152 this->
exec(
"END TRANSACTION;");
153 this->transaction =
false;
158 if(this->transaction) {
162 sqlite3_close(this->database);
163 this->database = NULL;
164 this->isopen =
false;
165 this->transaction =
false;
~Database(void)
destructor
bool isOpen(void)
returns true, when a database has been opened, false otherwise
void deactivateForeignKeys(void)
deactivates foreign keys
void open(const std::string &path, const OpenFlags flag=CREATE)
Opens a database with the passed flag.
void endTransaction(void)
commits a transaction
void beginTransaction(const TransactionFlags=DEFERRED)
Begins a transaction.
int getLastRowId(void)
gets the last ID, that has been inserted
Database(void)
empty constructor. Does not open a database
void close(void)
closes the database
void activateForeignKeys(void)
activates foreign keys
int exec(const std::string &sql)
executes a single statement without return values
void rollback(void)
executes a rollback on the current transaction