1//
2// Binding.cpp
3//
4// This sample demonstrates the Data library.
5//
6// Copyright (c) 2008, Applied Informatics Software Engineering GmbH.
7// and Contributors.
8//
9// SPDX-License-Identifier: BSL-1.0
10
11
12#include "Poco/SQL/Session.h"
13#include "Poco/SQL/SQLite/Connector.h"
14#include <vector>
15#include <iostream>
16
17
18using namespace Poco::SQL::Keywords;
19using Poco::SQL::Session;
20using Poco::SQL::Statement;
21
22
23struct Person
24{
25 std::string name;
26 std::string address;
27 int age;
28};
29
30
31int main(int argc, char** argv)
32{
33 // create a session
34 Session session("SQLite", "sample.db");
35
36 // drop sample table, if it exists
37 session << "DROP TABLE IF EXISTS Person", now;
38
39 // (re)create table
40 session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))", now;
41
42 // insert some rows
43 Person person =
44 {
45 "Bart Simpson",
46 "Springfield",
47 12
48 };
49
50 Statement insert(session);
51 insert << "INSERT INTO Person VALUES(?, ?, ?)",
52 use(person.name),
53 use(person.address),
54 use(person.age);
55
56 insert.execute();
57
58 person.name = "Lisa Simpson";
59 person.address = "Springfield";
60 person.age = 10;
61
62 insert.execute();
63
64 // a simple query
65 Statement select(session);
66 select << "SELECT Name, Address, Age FROM Person",
67 into(person.name),
68 into(person.address),
69 into(person.age),
70 range(0, 1); // iterate over result set one row at a time
71
72 while (!select.done())
73 {
74 select.execute();
75 std::cout << person.name << " " << person.address << " " << person.age << std::endl;
76 }
77
78 // another query - store the result in a container
79 std::vector<std::string> names;
80 session << "SELECT Name FROM Person",
81 into(names),
82 now;
83
84 for (std::vector<std::string>::const_iterator it = names.begin(); it != names.end(); ++it)
85 {
86 std::cout << *it << std::endl;
87 }
88
89 return 0;
90}
91