1 | // |
2 | // RecordSet.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/DateTime.h" |
14 | #include "Poco/SQL/SessionFactory.h" |
15 | #include "Poco/SQL/Session.h" |
16 | #include "Poco/SQL/RecordSet.h" |
17 | #include "Poco/SQL/Column.h" |
18 | #include "Poco/SQL/SQLite/Connector.h" |
19 | #include <iostream> |
20 | |
21 | |
22 | using namespace Poco::SQL::Keywords; |
23 | using Poco::DateTime; |
24 | using Poco::SQL::Session; |
25 | using Poco::SQL::Statement; |
26 | using Poco::SQL::RecordSet; |
27 | |
28 | |
29 | int main(int argc, char** argv) |
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), Birthday DATE)" , now; |
39 | |
40 | // insert some rows |
41 | DateTime bd(1980, 4, 1); |
42 | DateTime ld(1982, 5, 9); |
43 | session << "INSERT INTO Person VALUES('Bart Simpson', 'Springfield', 12, ?)" , use(bd), now; |
44 | session << "INSERT INTO Person VALUES('Lisa Simpson', 'Springfield', 10, ?)" , use(ld), now; |
45 | |
46 | // a simple query |
47 | Statement select(session); |
48 | select << "SELECT * FROM Person" ; |
49 | select.execute(); |
50 | |
51 | // create a RecordSet |
52 | RecordSet rs(select); |
53 | std::size_t cols = rs.columnCount(); |
54 | // print all column names |
55 | for (std::size_t col = 0; col < cols; ++col) |
56 | { |
57 | std::cout << rs.columnName(col) << std::endl; |
58 | } |
59 | // iterate over all rows and columns |
60 | bool more = rs.moveFirst(); |
61 | while (more) |
62 | { |
63 | for (std::size_t col = 0; col < cols; ++col) |
64 | { |
65 | std::cout << rs[col].convert<std::string>() << " " ; |
66 | } |
67 | std::cout << std::endl; |
68 | more = rs.moveNext(); |
69 | } |
70 | |
71 | return 0; |
72 | } |
73 | |