| 1 | // |
| 2 | // Tuple.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/SharedPtr.h" |
| 13 | #include "Poco/Tuple.h" |
| 14 | #include "Poco/SQL/SessionFactory.h" |
| 15 | #include "Poco/SQL/Session.h" |
| 16 | #include "Poco/SQL/SQLite/Connector.h" |
| 17 | #include <vector> |
| 18 | #include <iostream> |
| 19 | |
| 20 | |
| 21 | using namespace Poco::SQL::Keywords; |
| 22 | using Poco::SQL::Session; |
| 23 | using Poco::SQL::Statement; |
| 24 | |
| 25 | |
| 26 | int main(int argc, char** argv) |
| 27 | { |
| 28 | typedef Poco::Tuple<std::string, std::string, int> Person; |
| 29 | typedef std::vector<Person> People; |
| 30 | |
| 31 | // create a session |
| 32 | Session session("SQLite" , "sample.db" ); |
| 33 | |
| 34 | // drop sample table, if it exists |
| 35 | session << "DROP TABLE IF EXISTS Person" , now; |
| 36 | |
| 37 | // (re)create table |
| 38 | session << "CREATE TABLE Person (Name VARCHAR(30), Address VARCHAR, Age INTEGER(3))" , now; |
| 39 | |
| 40 | // insert some rows |
| 41 | People people; |
| 42 | people.push_back(Person("Bart Simpson" , "Springfield" , 12)); |
| 43 | people.push_back(Person("Lisa Simpson" , "Springfield" , 10)); |
| 44 | |
| 45 | Statement insert(session); |
| 46 | insert << "INSERT INTO Person VALUES(:name, :address, :age)" , |
| 47 | use(people), now; |
| 48 | |
| 49 | people.clear(); |
| 50 | |
| 51 | // a simple query |
| 52 | Statement select(session); |
| 53 | select << "SELECT Name, Address, Age FROM Person" , |
| 54 | into(people), |
| 55 | now; |
| 56 | |
| 57 | for (People::const_iterator it = people.begin(); it != people.end(); ++it) |
| 58 | { |
| 59 | std::cout << "Name: " << it->get<0>() << |
| 60 | ", Address: " << it->get<1>() << |
| 61 | ", Age: " << it->get<2>() <<std::endl; |
| 62 | } |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |