-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathreadme_example.cc
More file actions
64 lines (51 loc) · 1.9 KB
/
readme_example.cc
File metadata and controls
64 lines (51 loc) · 1.9 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
60
61
62
63
64
#define CATCH_CONFIG_MAIN
#include<iostream>
#include <catch2/catch.hpp>
#include <sqlite_modern_cpp.h>
using namespace sqlite;
using namespace std;
TEST_CASE("README Example Works", "[readme]") {
database db(":memory:");
db <<
"create table if not exists user ("
" _id integer primary key autoincrement not null,"
" age int,"
" name text,"
" weight real"
");";
db << "insert into user (age,name,weight) values (?,?,?);"
<< 20
<< u"bob"
<< 83.25;
int age = 22; float weight = 68.5; string name = "jack";
db << u"insert into user (age,name,weight) values (?,?,?);" // utf16 query string
<< age
<< name
<< weight;
REQUIRE(db.last_insert_rowid() != 0);
db << "select age,name,weight from user where age > ? ;"
<< 21
>> [&](int _age, string _name, double _weight) {
REQUIRE((_age == age && _name == name));
};
for(auto &&row : db << "select age,name,weight from user where age > ? ;" << 21) {
int _age;
string _name;
double _weight;
row >> _age >> _name >> _weight;
REQUIRE((_age == age && _name == name));
}
for(std::tuple<int, string, double> row : db << "select age,name,weight from user where age > ? ;" << 21) {
REQUIRE((std::get<int>(row) == age && std::get<string>(row) == name));
}
// selects the count(*) from user table
// note that you can extract a single culumn single row result only to : int,long,long,float,double,string,u16string
int count = 0;
db << "select count(*) from user" >> count;
REQUIRE(count == 2);
db << "select age, name from user where _id=1;" >> tie(age, name);
// this also works and the returned value will be automatically converted to string
string str_count;
db << "select count(*) from user" >> str_count;
REQUIRE(str_count == string{"2"});
}