13 Must(sqlite3_finalize(stmt_));
17 Must(sqlite3_step(stmt_));
22 Must(sqlite3_reset(stmt_));
23 Must(sqlite3_clear_bindings(stmt_));
27 int status = sqlite3_step(stmt_);
28 if (status == SQLITE_ROW) {
36 return sqlite3_column_int(stmt_, col);
40 return sqlite3_column_double(stmt_, col);
44 char* v =
const_cast<char*
>(
reinterpret_cast<const char *
>(
45 sqlite3_column_text(stmt_, col)));
47 *n = sqlite3_column_bytes(stmt_, col);
53 Must(sqlite3_bind_int(stmt_, i, val));
57 Must(sqlite3_bind_double(stmt_, i, val));
61 Must(sqlite3_bind_text(stmt_, i, val, -1, SQLITE_TRANSIENT));
65 Must(sqlite3_bind_blob(stmt_, i, val, n, SQLITE_TRANSIENT));
68SqlStatement::SqlStatement(sqlite3* db, std::string zSql)
72 Must(sqlite3_prepare_v2(db_, zSql.c_str(), -1, &stmt_, NULL));
75void SqlStatement::Must(
int status) {
76 if (status != SQLITE_OK && status != SQLITE_DONE && status != SQLITE_ROW) {
77 std::string err = sqlite3_errmsg(db_);
78 throw IOError(
"SQL error [" + zSql_ +
"]: " + err);
101 if (sqlite3_close(db_) == SQLITE_OK) {
115 std::ifstream ifile(path_.c_str());
117 remove(path_.c_str());
121 if (readonly_ && sqlite3_open_v2(path_.c_str(), &db_, SQLITE_OPEN_READONLY,
122 NULL) == SQLITE_OK) {
124 }
else if (sqlite3_open(path_.c_str(), &db_) == SQLITE_OK) {
128 throw IOError(
"Unable to create/open database " + path_);
142 sqlite3_stmt* statement;
144 sqlite3_prepare_v2(db_, sql.c_str(), -1, &statement, NULL);
145 if (result != SQLITE_OK) {
146 std::string error = sqlite3_errmsg(db_);
147 throw IOError(
"SQL error: " + sql +
" " + error);
150 result = sqlite3_step(statement);
151 if (result != SQLITE_DONE && result != SQLITE_ROW && result != SQLITE_OK) {
152 std::string error = sqlite3_errmsg(db_);
153 throw IOError(
"SQL error: " + sql +
" " + error);
156 sqlite3_finalize(statement);
162 sqlite3_stmt* statement;
165 sqlite3_prepare_v2(db_, sql.c_str(), -1, &statement, NULL);
166 if (check_query != SQLITE_OK) {
167 std::string error = sqlite3_errmsg(db_);
168 throw IOError(
"SQL error: " + error);
171 std::vector<StrList> results;
172 int cols = sqlite3_column_count(statement);
175 result = sqlite3_step(statement);
176 if (result != SQLITE_ROW) {
181 for (
int col = 0; col < cols; col++) {
183 char* ptr = (
char*)sqlite3_column_text(statement, col);
189 values.push_back(val);
191 results.push_back(values);
195 if (result != SQLITE_DONE && result != SQLITE_OK) {
196 std::string error = sqlite3_errmsg(db_);
197 throw IOError(
"SQL error: " + error);
199 sqlite3_finalize(statement);
for failed reading/writing to files, network connections, etc..
Thin wrapper class over sqlite3 prepared statements.
double GetDouble(int col)
Returns a double value for the specified column of the current query row.
boost::shared_ptr< SqlStatement > Ptr
int GetInt(int col)
Returns an int value for the specified column of the current query row.
char * GetText(int col, int *n)
Returns a byte array value for the specified column of the current query row.
void BindDouble(int i, double val)
Binds the templated sql parameter at index i to val.
void Reset()
Executes the prepared statement.
bool Step()
Step to next row of previously executed query.
void BindText(int i, const char *val)
Binds the templated sql parameter at index i to val.
void BindInt(int i, int val)
Binds the templated sql parameter at index i to val.
void Exec()
Executes the prepared statement.
void BindBlob(int i, const void *val, int n)
Binds the templated sql parameter at index i to the value pointed to by val.
void Overwrite()
Instead of opening a file of the specified name (if it already exists), overwrite it with a new empty...
void open()
Opens the sqlite database by either opening/creating a file (default) or creating/overwriting a file ...
void close()
Finishes any incomplete operations and closes the database.
void Execute(std::string cmd)
Execute an SQL command.
SqlStatement::Ptr Prepare(std::string sql)
Creates a sqlite prepared statement for the given sql.
SqliteDb(std::string path, bool readonly=false)
Creates a new Sqlite database to be stored at the specified path.
std::vector< StrList > Query(std::string cmd)
Execute an SQL query and return its results.
Code providing rudimentary logging capability for the Cyclus core.
taken directly from OsiSolverInterface.cpp on 2/17/14 from https://projects.coin-or....
std::vector< std::string > StrList