settings_db ia a very simplistic key/value pair database for use with the sq3 database layer. More...
#include <sq3_settings_db.hpp>
Public Member Functions | |
settings_db (std::string const &dbname) | |
Calls open(dbname). | |
settings_db () | |
Creates an unopened database. | |
~settings_db () | |
Closes this database. | |
virtual int | clear () |
Overridden to just empty the settings db. | |
int | clear (std::string const &where) |
Empties the database items matching the given WHERE clause. | |
void | set (std::string const &key, int val) |
Sets the given key/value pair. | |
void | set (std::string const &key, sqlite_int64 val) |
Sets the given key/value pair. | |
void | set (std::string const &key, bool val) |
Sets the given key/value pair. | |
void | set (std::string const &key, double val) |
Sets the given key/value pair. | |
void | set (std::string const &key, std::string const &val) |
Sets the given key/value pair. | |
void | set (std::string const &key, char const *val) |
Sets the given key/value pair. | |
bool | get (std::string const &key, int &val) |
Fetches the given key from the db. | |
bool | get (std::string const &key, sqlite_int64 &val) |
See get(string,int). | |
bool | get (std::string const &key, bool &val) |
See get(string,int). | |
bool | get (std::string const &key, double &val) |
See get(string,int). | |
bool | get (std::string const &key, std::string &val) |
See get(string,int). |
settings_db ia a very simplistic key/value pair database for use with the sq3 database layer.
It is intended to be used as a simple config-file class.
Usage:
settings_db db("my.db"); db.set("one", 1 ); db.set("two", 2.0 ); db.set("a_string", "a string" );
std::string sval; assert( db.get( "a_string", sval ) );
Obviously, an assert may be too harsh for what you're doing.
When doing lots of set() calls you can gain a lot of speed by adding an sq3::transaction before you start and calling commit() on that transaction when you're done.
Definition at line 32 of file sq3_settings_db.hpp.
sq3::settings_db::settings_db | ( | std::string const & | dbname | ) | [explicit] |
Calls open(dbname).
Use is_open() to find out if the open succeeded.
Definition at line 10 of file sq3_settings_db.cpp.
sq3::settings_db::settings_db | ( | ) |
Creates an unopened database.
You must call open() before you can use this object.
Definition at line 5 of file sq3_settings_db.cpp.
int sq3::settings_db::clear | ( | ) | [virtual] |
Overridden to just empty the settings db.
Does not remove the db file. Returns SQLITE_OK on success, else false.
Reimplemented from sq3::database.
Definition at line 20 of file sq3_settings_db.cpp.
int sq3::settings_db::clear | ( | std::string const & | where | ) |
Empties the database items matching the given WHERE clause.
Does not remove the db file.
'where' should be a full SQL where statement, e.g.:
"WHERE KEY LIKE 'x%'"
The field names in this db are KEY and VALUE.
Definition at line 25 of file sq3_settings_db.cpp.
bool sq3::settings_db::get | ( | std::string const & | key, | |
int & | val | |||
) |
Fetches the given key from the db.
If it is found, it is converted to the data type of val, val is assigned that value, and true is returned. If false is returned then val is unchanged.
Definition at line 91 of file sq3_settings_db.cpp.
References sq3::statement::bind(), sq3::statement::execute(), and sq3::rc_is_okay().
bool sq3::settings_db::get | ( | std::string const & | key, | |
sqlite_int64 & | val | |||
) |
See get(string,int).
Definition at line 106 of file sq3_settings_db.cpp.
bool sq3::settings_db::get | ( | std::string const & | key, | |
bool & | val | |||
) |
See get(string,int).
Definition at line 120 of file sq3_settings_db.cpp.
bool sq3::settings_db::get | ( | std::string const & | key, | |
double & | val | |||
) |
See get(string,int).
Definition at line 140 of file sq3_settings_db.cpp.
bool sq3::settings_db::get | ( | std::string const & | key, | |
std::string & | val | |||
) |
See get(string,int).
Definition at line 154 of file sq3_settings_db.cpp.