-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathexample.cpp
More file actions
59 lines (50 loc) · 1.51 KB
/
example.cpp
File metadata and controls
59 lines (50 loc) · 1.51 KB
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
51
52
53
54
55
56
57
58
59
#include<iostream>
#include<string>
#include<tuple>
#include "sqlite_modern_cpp.h"
using namespace sqlite;
using namespace std;
int main(){
try {
// creates a database file 'dbfile.db' if not exists
database db("dbfile.db");
// executes the query and creates a 'user' table
db <<
"create table if not exists user ("
" age int,"
" name text,"
" weight real"
");";
// inserts a new user and binds the values to ?
// note that only types allowed for bindings are :
// int ,long, long long, float, double
// string , wstring
db << "insert into user (age,name,weight) values (?,?,?);"
<< 20
<< "bob"
<< 83.0;
db << "insert into user (age,name,weight) values (?,?,?);"
<< 21
<< L"jack"
<< 68.5;
// slects from table user on a condition ( age > 18 ) and executes
// the lambda for every row returned .
db << "select age,name,weight from user where age > ? ;"
<< 18
>> [&](int age, string name, double weight) {
cout << age << ' ' << name << ' ' << weight << endl;
};
// selects the count(*) of table user
// note that you can extract a single culumn single row answer only to : int,long,long,float,double,string,wstring
int count = 0;
db << "select count(*) from user" >> count;
cout << "cout : " << count << endl;
// this also works and the returned value will automatically converted to string
string scount;
db << "select count(*) from user" >> scount;
cout << "scount : " << scount << endl;
}
catch (exception& e){
cout << e.what() << endl;
}
}