db_bridge/postgresql_db.cpp
changeset 15 5a0ca1f9a2f1
parent 14 862d63715611
child 17 b3731a25b9ec
equal deleted inserted replaced
14:862d63715611 15:5a0ca1f9a2f1
     2  * db_bridge/postgresql_db.cpp
     2  * db_bridge/postgresql_db.cpp
     3  * Copyright 2008 (C) Markus Broeker
     3  * Copyright 2008 (C) Markus Broeker
     4  */
     4  */
     5 
     5 
     6 #include <postgresql_db.h>
     6 #include <postgresql_db.h>
       
     7 #include <exception>
     7 
     8 
     8 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
     9 PostgreSQL_DB::PostgreSQL_DB (std::string srv, std::string usr, std::string pwd, std::string db)
     9 {
    10 {
    10     server = (srv != "") ? "host=" + srv : "";
    11     server = (srv != "") ? "host=" + srv : "";
    11     user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
    12     user = (usr != "") ? "user=" + usr : "user=" + std::string (getenv ("USER"));
    41 Abstract_DB::DB_RESULT PostgreSQL_DB::query (std::string sql_string)
    42 Abstract_DB::DB_RESULT PostgreSQL_DB::query (std::string sql_string)
    42 {
    43 {
    43     Abstract_DB::DB_ROW vec;
    44     Abstract_DB::DB_ROW vec;
    44     Abstract_DB::DB_RESULT rows;
    45     Abstract_DB::DB_RESULT rows;
    45     PGresult *res;
    46     PGresult *res;
    46     int i, j, nFields;
    47     int i, j;
       
    48     int nFields, nRows;
    47 
    49 
    48     /*
    50     /*
    49      * send SQL query
    51      * send SQL query
    50      */
    52      */
    51     res = PQexec (conn, sql_string.c_str ());
    53     res = PQexec (conn, sql_string.c_str ());
    52 
    54 
    53     nFields = PQnfields (res);
    55     nFields = PQnfields (res);
       
    56     nRows = PQntuples (res);
    54 
    57 
    55     std::cout << "Die Abfrage ergab " << nFields << " Zeilen." << std::endl;
    58     std::cout << "Die Abfrage hat " << nFields << " Spalte(n) und " << nRows << " Reihe(n)." << std::endl;
    56 
    59 
    57     /*
    60     /*
    58      * push everything into a vector< vector<string> >
    61      * push everything into a vector< vector<string> >
    59      */
    62      */
    60     for (i = 0; i < PQntuples (res); i++) {
    63     for (i = 0; i < nRows; i++) {
    61         for (j = 0; j < nFields; j++) {
    64         for (j = 0; j < nFields; j++) {
    62             vec.push_back (PQgetvalue (res, i, j));
    65             vec.push_back (PQgetvalue (res, i, j));
    63         }
    66         }
    64         rows.push_back (vec);
    67         rows.push_back (vec);
    65         vec.clear ();
    68         vec.clear ();