Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. c++ and PostgreSql
Forum Updated to NodeBB v4.3 + New Features

c++ and PostgreSql

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 478 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    lincoln
    wrote on 23 Mar 2020, 00:52 last edited by lincoln
    #1

    Hello friends, if my question is not strictly about Qt, but about pure c ++, I am trying to connect to a PostgreeSql database, and then I have managed to connect my app to the database, but at the time of displaying the data it does not show me anything .
    I already added the necessary libraries but even so it does not show me the data, I would appreciate your opinion, thank you.

    Note:

    • I leave the code and some screenshots.

    • it does not show me the rows but it does connect to the database.

    source code:

    #include <iostream>
    #include "libpq-fe.h"
    
    using namespace std;
    
    int main()
    {
      PGconn *conn;
      PGresult *res;
      conn=PQsetdbLogin("localhost","5432",nullptr,nullptr,"dbimagenes","postgres","2358965");
      if(PQstatus(conn)!=CONNECTION_BAD)
        {
          cout<<"Estamos conectdos a PostgreSql!"<<endl;
          res=PQexec(conn,"SELECT * FROM imagenes");
          if(res!=nullptr)
            {
              int tuplas=PQntuples(res);
              int campos=PQnfields(res);
              cout<<"Numero de filas: "<<tuplas<<endl;
              cout<<"Numero de campos: "<<campos<<endl;
              cout<<"Los nombres de los campos son:"<<endl;
              for(int i=0;i<campos;++i){
                  cout<<PQfname(res,i)<<" | ";
                }
              cout<<endl<<"Contenido de la tabla"<<endl;
              for(int i=0;i<tuplas;++i){
                  for(int j=0;j<campos;++j){
                      cout<<PQgetvalue(res,i,j)<<" | ";
                    }
                  cout<<endl;
                }
            }
          PQclear(res);
    
        }else{
          cout<<"Error de conexion."<<endl;
          return 0;
        }
      PQfinish(conn);
      return 0;
    }
    
    

    File .pro

    TEMPLATE = app
    CONFIG += console c++11
    CONFIG -= app_bundle
    CONFIG -= qt
    
    SOURCES += \
            main.cpp
    
    #win32:CONFIG(release, debug|release): LIBS += -L$$PWD/lib/ -llibpq
    #else:win32:CONFIG(debug, debug|release): LIBS += -L$$PWD/lib/ -llibpq
    
    #INCLUDEPATH += $$PWD/include
    #DEPENDPATH += $$PWD/include
    
    INCLUDEPATH +=$$PWD/include
    LIBS+=-L$$PWD/lib/ -llibpq
    

    resultado:

    79dbbf43-8425-46d8-a2d0-6f9fc09c5b77-image.png

    • As you can see here it shows me the number of rows and the table headers, but not the data.

    Solitary wolf

    1 Reply Last reply
    0
    • K Offline
      K Offline
      Kent-Dorfman
      wrote on 23 Mar 2020, 01:37 last edited by
      #2

      Just as a side curiosity, why are you using the lowest level access API? There also exist libpqxx, and Qt database API.

      I'd suggest rewriting your access using the pur Qt API, or at the very least, the libpqxx API.

      1 Reply Last reply
      2

      1/2

      23 Mar 2020, 00:52

      • Login

      • Login or register to search.
      1 out of 2
      • First post
        1/2
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved