44 lines
1.1 KiB
C++
44 lines
1.1 KiB
C++
#include "Database.hpp"
|
|
|
|
using namespace std;
|
|
|
|
DatabaseHandle::DatabaseHandle(const string& filename) {
|
|
if (SQLITE_OK != sqlite3_open(filename.c_str(), &dbh))
|
|
throw DatabaseError();
|
|
}
|
|
|
|
DatabaseHandle::~DatabaseHandle() {
|
|
sqlite3_close(dbh);
|
|
}
|
|
|
|
PreparedStatement DatabaseHandle::prepareStatement(const std::string& statement) {
|
|
return PreparedStatement(dbh, statement);
|
|
}
|
|
|
|
|
|
PreparedStatement::PreparedStatement(sqlite3 *dbh, const std::string& statement) {
|
|
const char *tail;
|
|
int rc = sqlite3_prepare_v2(dbh, statement.c_str(), -1, &stmt, &tail);
|
|
if (SQLITE_OK != rc)
|
|
throw DatabaseError();
|
|
}
|
|
|
|
PreparedStatement::~PreparedStatement() {
|
|
sqlite3_finalize(stmt);
|
|
}
|
|
|
|
result_t PreparedStatement::next() {
|
|
int rc = sqlite3_step(stmt);
|
|
int ncol = sqlite3_column_count(stmt);
|
|
result_t result;
|
|
if (SQLITE_ROW == rc) {
|
|
for (int i = 0; i < ncol; i++) {
|
|
string key = sqlite3_column_name(stmt, i);
|
|
string value = string(reinterpret_cast<const char*>(sqlite3_column_text(stmt, i)));
|
|
result.insert(pair<string, string>(key, value));
|
|
}
|
|
}
|
|
return result;
|
|
}
|
|
|