-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtests.cpp
More file actions
78 lines (59 loc) · 2.51 KB
/
tests.cpp
File metadata and controls
78 lines (59 loc) · 2.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
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include "libsqlite.hpp"
int main()
{
try
{
sqlite::sqlite db(":memory:"); // Open an in-memory database
// Will auto close on destruction
sqlite::statement_ptr s = db.get_statement();
s->set_sql("CREATE TABLE test(id INTEGER PRIMARY KEY, text TEXT)");
s->exec(); //exec calls prepare and step at once.
// When a shared_ptr like statement_pt is assigned another value, it deletes
// its contents. So s = db.get_statement(); Would do almost the same, except
// it would clear its bindings, which are NOT done now! As we don't have bindings,
// we don't care right now.
s->reset();
// INSERT example without bind.
s->set_sql("INSERT INTO test(text) VALUES('test')");
s->exec();
std::cout << "Inserted into test without binding values." << std::endl;
// Reset the statement again.
s->reset();
std::string special_text = "text with ap'ostrophes and sla/shes in both \\ \
directions are handled perfectly with bind!";
s->set_sql("INSERT INTO test(text) VALUES(?)");
s->prepare();
s->bind(1, special_text); // sqlite starts counting at 0
s->step();
std::cout << "Inserted into test with binding values." << std::endl;
//Another reset. Bear in mind that bindings are NOT cleared.
s->reset();
s->set_sql("INSERT into test(text) VALUES(?5)");
s->prepare();
s->bind(5, special_text); // If we specify an integer literal after the
// question mark, we can use that number, which is
// very useful in longer queries.
s->step();
std::cout << "Inserted into test with binding values to integer literal." << std::endl;
s->reset();
s->prepare(); // You can reuse your sql of the previous query.
s->bind(5, "other test, reuse text");
s->step();
std::cout << "Inserted into test with reuse of sql." << std::endl;
s->reset(); // We know what this does...
// SELECT example.
s->set_sql("SELECT id, text FROM test");
s->prepare();
std::cout << "Retrieve values:" << std::endl;
while(s->step())
{
std::cout << s->get_int(0) << " " << s->get_text(1) << std::endl;
}
}
catch(sqlite::exception e)
{
std::cerr << e.what() << std::endl;
}
return 0;
}