-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample.cpp
51 lines (43 loc) · 1.38 KB
/
example.cpp
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
#include "sqlite.hpp"
#include <iostream>
int main()
{
/// Opening a new connection
sqlite::Connection connection("example.db");
/// Executing a statement
connection.Statement("CREATE TABLE IF NOT EXISTS example ("
"id INTEGER PRIMARY KEY AUTOINCREMENT, "
"textData TEXT, "
"intData INTEGER, "
"floatData REAL)");
/// Executing a statement with parameters
connection.Statement("INSERT INTO example (textData, intData, floatData) "
"VALUES (?,?,?)",
"Hello world",
1,
1.23);
/// Executing a query
sqlite::Result result = connection.Query("SELECT * FROM example");
/// Iterating through the result rows
while(result.Next())
{
std::cout
<< result.Get<int>(0) << " "
<< result.Get<std::string>(1) << " "
<< result.Get<int>(2) << " "
<< result.Get<float>(3) << std::endl;
}
/// Exceptions
try
{
/// Deliberate mistake here ↓
(void)connection.Query("SELECCT textData FROM example");
}
catch(const sqlite::Error& e)
{
std::cout << e.what() << std::endl;
}
/// Copy data into backup.db
connection.Backup("backup.db");
return 0;
}