|
| 1 | +sqlite modern cpp wrapper |
| 2 | +==== |
| 3 | + |
| 4 | +This library is lightweight wrapper around sqlite C api . |
| 5 | + |
| 6 | +```c++ |
| 7 | +#include<iostream> |
| 8 | +#include "sqlite_modern_cpp.h" |
| 9 | +using namespace sqlite; |
| 10 | +using namespace std; |
| 11 | + |
| 12 | +int main(){ |
| 13 | + try { |
| 14 | + // creates a database file 'dbfile.db' if not exists |
| 15 | + database db("dbfile.db"); |
| 16 | + |
| 17 | + // executes the query and creates a 'user' table |
| 18 | + db << |
| 19 | + "create table user (" |
| 20 | + " age int," |
| 21 | + " name text," |
| 22 | + " weight real" |
| 23 | + ");"; |
| 24 | + |
| 25 | + // inserts a new user and binds the values to ? |
| 26 | + // note that only types allowed for bindings are |
| 27 | + // 1 - numeric types( int ,long ,long long, float, double , ... ) |
| 28 | + // 2 - string , wstring |
| 29 | + db << "insert into user (age,name,weight) values (?,?,?);" |
| 30 | + << 20 |
| 31 | + << "bob" |
| 32 | + << 83.0; |
| 33 | + |
| 34 | + db << "insert into user (age,name,weight) values (?,?,?);" |
| 35 | + << 21 |
| 36 | + << L"jak" |
| 37 | + << 68.5; |
| 38 | + |
| 39 | + // slects from table user on a condition ( age > 18 ) and executes |
| 40 | + // the body of magid_mapper for every row returned . |
| 41 | + // node : magic_mapper is just a simple macro , the next sample is |
| 42 | + // equivalent to this one without the use of magic_mapper macro |
| 43 | + db << "select age,name,weight from user where age > ? ;" |
| 44 | + << 18 |
| 45 | + >> magic_mapper(int age, string name, double weight) { |
| 46 | + cout << age << ' ' << name << ' ' << weight << endl; |
| 47 | + }; |
| 48 | + |
| 49 | + db << "select age,name,weight from user where age > ? ;" |
| 50 | + << 18 |
| 51 | + >> function<void(int,string,double)>([&](int age, string name, double weight) { |
| 52 | + cout << age << ' ' << name << ' ' << weight << endl; |
| 53 | + }); |
| 54 | + |
| 55 | + // i am currently working on a solution to avoid magic mapper |
| 56 | + // i future i want to this syntax also work |
| 57 | + /* |
| 58 | + db << "select age,name,weight from user where age > ? ;" |
| 59 | + << 18 |
| 60 | + >> [&](int age, string name, double weight) { |
| 61 | + cout << age << ' ' << name << ' ' << weight << endl; |
| 62 | + }; |
| 63 | + */ |
| 64 | + |
| 65 | + } |
| 66 | + catch (exception& e){ |
| 67 | + cout << e.what() << endl; |
| 68 | + } |
| 69 | +} |
| 70 | +``` |
0 commit comments