25 SQLiteException StatementNotPrepared(
"The statement has not been prepared or it has been finalized.");
27 inline void Statement::checkPrepared()
const {
29 throw StatementNotPrepared;
35 throw DatabaseNotOpened;
38 this->finalized =
true;
39 this->statement = NULL;
43 this->checkPrepared();
50 if(!this->finalized && this->statement) {
51 std::cout <<
"The statement has not been finalized! Memory leak!" << std::endl;
54 this->lastResult = sqlite3_prepare_v2(this->db.database, str.c_str(), str.size(),
55 &this->statement, NULL);
58 if(this->lastResult == SQLITE_OK) {
59 this->finalized =
false;
65 StepValue Statement::step() {
66 this->checkPrepared();
68 this->lastResult = sqlite3_step(this->statement);
70 switch(this->lastResult) {
75 if(this->columns.size() == 0) {
77 index < sqlite3_column_count(this->statement);
79 const char*p = (
const char*)
80 sqlite3_column_name(this->statement, index);
82 this->columns.insert(this->columns.begin(),
83 std::pair<std::string, int>(p, index));
94 this->checkPrepared();
96 return (this->step() == ROW);
100 this->checkPrepared();
102 if(this->columns.find(name) == this->columns.end()) {
103 std::cout <<
"Unknown column " << name << std::endl;
105 return this->
getInt(this->columns.find(name)->second);
109 this->checkPrepared();
111 return sqlite3_column_int(this->statement, index);
115 this->checkPrepared();
117 out = this->
getInt(index);
121 this->checkPrepared();
123 const char* p = (
const char*)sqlite3_column_text(this->statement, index);
125 return std::string(p);
132 this->checkPrepared();
134 if(this->columns.find(name) == this->columns.end()) {
135 std::cout <<
"Unknown column " << name << std::endl;
137 return this->
getString(this->columns.find(name)->second);
141 this->checkPrepared();
147 this->checkPrepared();
149 if(this->columns.find(name) == this->columns.end()) {
150 std::cout <<
"Unknown column " << name << std::endl;
152 return this->
getDouble(this->columns.find(name)->second);
156 this->checkPrepared();
158 return sqlite3_column_double(this->statement, index);
162 this->checkPrepared();
168 this->checkPrepared();
170 this->lastResult = sqlite3_bind_int(this->statement, index, value);
174 this->checkPrepared();
176 this->lastResult = sqlite3_bind_text(this->statement, index, value.c_str(), value.size(), NULL);
180 this->checkPrepared();
182 this->lastResult = sqlite3_bind_double(this->statement, index, value);
186 this->checkPrepared();
188 this->lastResult = sqlite3_bind_null(this->statement, index);
192 if(this->statement && !this->finalized) {
193 sqlite3_finalize(this->statement);
194 this->columns.clear();
195 this->statement = NULL;
196 this->finalized =
true;
void bindNull(const int n)
Binds the nth parameter with the null value.
bool fetchRow(void)
fetches the next row
bool isOpen(void)
returns true, when a database has been opened, false otherwise
std::string getString(const std::string &column) const
gets a column as string
int getInt(const std::string &column) const
gets a column as integer
void bindString(const int n, const std::string &value)
Binds the nth parameter with the passed value as string.
void exec(void)
executes the prepared statement
Statement(Database &db)
Default constructor.
void bindDouble(const int n, const double value)
Binds the nth parameter with the passed value as integer.
void prepare(const std::string &sql)
prepares a string as statement
double getDouble(const std::string &column) const
gets a column as double
void finalize(void)
releases any allocated resources. Use it, when you want to reuse a statement object.
void bindInt(const int n, const int value)
Binds the nth parameter with the passed value as integer.