-
Notifications
You must be signed in to change notification settings - Fork 0
/
sqlite_example.cpp
50 lines (36 loc) · 1.01 KB
/
sqlite_example.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
#include <string>
#include <vector>
#include <stdexcept>
#include <mmx/sqlite.hpp>
struct test {
int id, c0, c1;
friend mmx::sqlite::statement& operator << (mmx::sqlite::statement& sql, const test& t) {
sql << t.id << t.c0 << t.c1;
return sql;
}
friend mmx::sqlite::statement& operator >> (mmx::sqlite::statement& sql, test& t) {
sql >> t.id >> t.c0 >> t.c1;
return sql;
}
friend std::ostream& operator << (std::ostream& os, const test& t) {
os << t.id << ' ' << t.c0 << ' ' << t.c1;
return os;
}
};
int main() {
try {
mmx::sqlite db;
db.open("test.sqlite3", SQLITE_OPEN_CREATE | SQLITE_OPEN_READWRITE);
db.query("create table test(id int, c0 int, c1 int);");
auto results = db.query<test>("select * from test limit 0,5");
std::cout << "size: " << results.size() << "\n";
for (const auto& m : results)
std::cout << m << "\n";
} catch (const mmx::db_error& e) {
std::cout << e.what() << "\n";
} catch (...) {
std::cout << "serious error?\n";
}
return 0;
}