sqlitepp
A small sqlite3 C++ library
 All Classes Functions Pages
example.cpp
1 #include "sqlitepp.h"
2 #include <iostream>
3 
4 int main() {
5  std::cout << "Opening database test.db..." << std::endl;
6  // :memory: opens an in-memory database
7  // use file paths if you want to open a sqlite database file
8  sqlitepp::Database db(":memory:");
9 
10  std::cout << "Creating table..." << std::endl;
11  int v = db.exec("CREATE TABLE users (name TEXT, password TEXT);");
12 
13  std::cout << "Inserting data directly..." << std::endl;
14  db.exec("INSERT INTO users (name, password) VALUES ('paul', 'test');");
15 
16  std::cout << "Inserting data by prepared statement..." << std::endl;
17  sqlitepp::Statement st(db);
18  st.prepare("INSERT INTO users (name, password) VALUES (?, ?);");
19  st.bindString(1, "steve");
20  st.bindString(2, "this_is_a_password");
21  st.exec();
22 
23  std::cout << "Selecting all users..." << std::endl;
24  st.prepare("SELECT * FROM users;");
25  while(st.fetchRow()) {
26  std::cout << "Username: " << st.getString("name")
27  << ", password: " << st.getString("password") << std::endl;
28  }
29 
30  try {
32  db2.exec("COMMIT;");
33 
34  std::cout << "Exceptions doen't work." << std::endl;
35  } catch(sqlitepp::SQLiteException& e) {
36  std::cout << "Exceptions work." << std::endl;
37  }
38 
39  try {
40  sqlitepp::Statement st(db);
41  st.bindInt(1, 4);
42 
43  std::cout << "Exceptions doen't work." << std::endl;
44  } catch(sqlitepp::SQLiteException& e) {
45  std::cout << "Exceptions work." << std::endl;
46  }
47 }
48 
A prepared statement.
Definition: sqlitepp.h:183
int exec(const std::string &sql)
executes a single statement without return values
Definition: database.cpp:98
The main database class.
Definition: sqlitepp.h:73