-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
48 lines (38 loc) · 1.49 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
#include "sqlitepp.h"
#include <iostream>
int main() {
std::cout << "Opening database test.db..." << std::endl;
// :memory: opens an in-memory database
// use file paths if you want to open a sqlite database file
sqlitepp::Database db(":memory:");
std::cout << "Creating table..." << std::endl;
int v = db.exec("CREATE TABLE users (name TEXT, password TEXT);");
std::cout << "Inserting data directly..." << std::endl;
db.exec("INSERT INTO users (name, password) VALUES ('paul', 'test');");
std::cout << "Inserting data by prepared statement..." << std::endl;
sqlitepp::Statement st(db);
st.prepare("INSERT INTO users (name, password) VALUES (?, ?);");
st.bindString(1, "steve");
st.bindString(2, "this_is_a_password");
st.exec();
std::cout << "Selecting all users..." << std::endl;
st.prepare("SELECT * FROM users;");
while(st.fetchRow()) {
std::cout << "Username: " << st.getString("name")
<< ", password: " << st.getString("password") << std::endl;
}
try {
sqlitepp::Database db2;
db2.exec("COMMIT;");
std::cout << "Exceptions doen't work." << std::endl;
} catch(sqlitepp::SQLiteException& e) {
std::cout << "Exceptions work." << std::endl;
}
try {
sqlitepp::Statement st(db);
st.bindInt(1, 4);
std::cout << "Exceptions doen't work." << std::endl;
} catch(sqlitepp::SQLiteException& e) {
std::cout << "Exceptions work." << std::endl;
}
}