-
Notifications
You must be signed in to change notification settings - Fork 166
Expand file tree
/
Copy pathblob_example.cc
More file actions
61 lines (52 loc) · 1.76 KB
/
blob_example.cc
File metadata and controls
61 lines (52 loc) · 1.76 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
#include <iostream>
#include <cstdlib>
#include <vector>
#include <string>
#include <sqlite_modern_cpp.h>
using namespace sqlite;
using namespace std;
int main()
{
try
{
database db(":memory:");
db << "CREATE TABLE person (name TEXT, numbers BLOB);";
db << "INSERT INTO person VALUES (?, ?)" << "bob" << blob(vector<int> { 1, 2, 3});
db << "INSERT INTO person VALUES (?, ?)" << "jack" << blob(vector<char> { '1', '2', '3'});
db << "INSERT INTO person VALUES (?, ?)" << "sara" << blob(vector<double> { 1.0, 2.0, 3.0});
// Extract to lvalue blob
vector<int> numbers_bob;
db << "SELECT numbers from person where name = ?;" << "bob" >> blob(numbers_bob);
if(numbers_bob.size() != 3 || numbers_bob[0] != 1 || numbers_bob[1] != 2 || numbers_bob[2] != 3) {
cout << "Bad result on line " << __LINE__ << endl;
exit(EXIT_FAILURE);
}
// Extract to lambda blob parameter
vector<char> numbers_jack;
db << "SELECT numbers from person where name = ?;" << "jack" >> [](blob_t<vector<char>> numbers_jack) {
if(numbers_jack.vec.size() != 3 || numbers_jack.vec[0] != '1'
|| numbers_jack.vec[1] != '2' || numbers_jack.vec[2] != '3') {
cout << "Bad result on line " << __LINE__ << endl;
exit(EXIT_FAILURE);
}
};
// Extract to rvalue blob
auto numbers_sara = db << "SELECT numbers from person where name = ?;" << "sara" >> blob(vector<double>());
if(numbers_sara.size() != 3 || numbers_sara[0] != 1 || numbers_sara[1] != 2 || numbers_sara[2] != 3) {
cout << "Bad result on line " << __LINE__ << endl;
exit(EXIT_FAILURE);
}
}
catch(sqlite_exception e)
{
cout << "Unexpected error " << e.what() << endl;
exit(EXIT_FAILURE);
}
catch(...)
{
cout << "Unknown error\n";
exit(EXIT_FAILURE);
}
cout << "OK\n";
exit(EXIT_SUCCESS);
}