User Tools

Site Tools


Differences

This shows you the differences between two versions of the page.

Link to this comparison view

cpp:pqxx [2019-03-04 18:02] (current)
ziggi created
Line 1: Line 1:
 +
 +=====libpqxx sample=====
 +
 +
 +<code c++ pqxx.cpp>​
 +/*
 + * pqxx.cpp
 + *
 + * Copyright 2019 Oleg Borodin ​ <​borodin@unix7.org>​
 + *
 + * This program is free software; you can redistribute it and/or modify
 + * it under the terms of the GNU General Public License as published by
 + * the Free Software Foundation; either version 2 of the License, or
 + * (at your option) any later version.
 + *
 + * This program is distributed in the hope that it will be useful,
 + * but WITHOUT ANY WARRANTY; without even the implied warranty of
 + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. ​ See the
 + * GNU General Public License for more details.
 + *
 + * You should have received a copy of the GNU General Public License
 + * along with this program; if not, write to the Free Software
 + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
 + * MA 02110-1301, USA.
 + *
 + *
 + */
 +
 +#include <​iostream>​
 +#include <​pqxx/​pqxx>​
 +
 +int main(int, char *argv[]) {
 +
 +    try {
 +        pqxx::​connection conn("​host=localhost dbname=lorem user=pgsql"​);​
 +        pqxx::work txn(conn);
 +
 +        pqxx::​result res = txn.exec("​select id, name, password from users order by id");
 +        std::cout << "# res size " << res.size() << "​\n";​
 +
 +        for (auto row: res) {
 +            for (auto field: row) {
 +                std::cout << field.c_str() << "​\t";​
 +            }
 +            std::cout << std::endl;
 +        }
 +    }
 +    catch (const pqxx::​sql_error &e) {
 +        std::cerr << "# sql error: " << e.what() << std::endl;
 +        std::cerr << "# query was: " << e.query() << std::endl;
 +        return 2;
 +    }
 +    catch (const std::​exception &e) {
 +        std::cerr << "# error: " << e.what() << std::endl;
 +        return 1;
 +    }
 +}
 +</​code>​
 +
 +===Out===
 +
 +<​file>​
 +$ sudo ./pgxx
 +# res size 9
 +1 qwerty 12345
 +2 nklokko 12345
 +3 rosetta 12345
 +4 user2  ​       12345
 +5 skyekr 12345
 +6 kirluke 12345
 +7 koeplin 12345
 +8 mhowe 12345
 +12 qwerty1 12345
 +</​file>​