Created
October 16, 2016 21:36
-
-
Save JAChapmanII/7b35f37431b7098ebda7ffe85b2e2fc0 to your computer and use it in GitHub Desktop.
db_ex
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <iostream> | |
using std::cout; | |
using std::endl; | |
#include <string> | |
using std::string; | |
#include "db/sqlite.hpp" | |
struct Test { | |
int a; | |
string b; | |
}; | |
namespace sqlite { | |
template<> void Statement::bind<Test>(int idx, Test t) { | |
this->bind(idx + 0, t.a); | |
this->bind(idx + 1, t.b); | |
} | |
template<> Test Result::get<Test>(int idx) { | |
Test t; | |
t.a = this->getInteger(idx + 0); | |
t.b = this->getString(idx + 1); | |
return t; | |
} | |
} | |
int main(int, char **) { | |
sqlite::Database db{"./ex.db"}; | |
cout << "SELECT 1: " << *(db.executeScalar<int>("SELECT 1")) << endl; | |
if(db.tableExists("tmp")) { | |
cout << "table `tmp` exists" << endl; | |
} else { | |
cout << "table `tmp` does not exist, creating it" << endl; | |
db.executeVoid("CREATE TABLE tmp(val INT)"); | |
} | |
cout << "truncating `tmp`" << endl; | |
db.executeVoid("DELETE FROM tmp"); | |
for(int i = 0; i < 10; ++i) | |
db.executeVoid("INSERT INTO tmp VALUES (?1)", i); | |
auto values = db.executeVector<int>("SELECT * FROM tmp"); | |
for(auto &value : values) { | |
cout << "value: " << value << endl; | |
} | |
Test t; | |
t.a = 18; | |
t.b = "test"; | |
if(db.tableExists("tests")) { | |
cout << "table `tests` exists" << endl; | |
} else { | |
cout << "table `tests` does not exist, creating it" << endl; | |
db.executeVoid("CREATE TABLE tests(a INT, b VARCHAR)"); | |
} | |
db.executeVoid("INSERT INTO tests VALUES(?1, ?2)", t); | |
auto tRes = db.executeScalar<Test>( | |
"SELECT * FROM tests WHERE a = ?1", t.a); | |
if(tRes) { | |
cout << "found: " << endl; | |
Test z = *tRes; | |
cout << "z.a: " << z.a << ", z.b: " << z.b << endl; | |
} else { | |
cout << "not found" << endl; | |
} | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment