Unsolved c++ and PostgreSql
-
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:
- As you can see here it shows me the number of rows and the table headers, but not the data.
-
-
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.