Problems querying on Postgres



  • Dialog_Login.h*******
    #include <QDialog>
    #include <QtSql>
    #include <QSqlDatabase>

    class Dialog_Login;
    }

    class Dialog_Login : public QDialog
    {
    Q_OBJECT

    public:
    explicit Dialog_Login(QWidget *parent = 0);
    ~Dialog_Login();

    void Rellenar_Usuarios();
    QSqlQueryModel *Modelo1;
    QSqlDatabase db;

    Dialog_Login.cpp*

    void Dialog_Login::Rellenar_Usuarios()
    {

    db = QSqlDatabase::addDatabase("QPSQL");
    db.setDatabaseName("*******");
    db.setHostName("********");
    db.setUserName("*****");
    db.setPassword("*****");
    db.setPort(5432);
    
    
    
    
    
    if(db.open())
    
    {
        QSqlQuery query;
    
    
    
          query.exec("SELECT id_usuario FROM Usuario");
    
          ui->tableWidget->setRowCount(1);
    
                     ui->tableWidget->setColumnCount(1);
                     ui->tableWidget->setSizePolicy(QSizePolicy::Expanding,QSizePolicy::Expanding);
    
    
                     ui->tableWidget->setHorizontalHeaderLabels(QString("Usuarios").split(";"));
    
                 int i=0;
                 while (query.next()) {
    
    
                   ui->tableWidget->setItem(i,0,new QTableWidgetItem(query.value(1).toString()));
    
    
                      i++;
                 ui->tableWidget->insertRow(i);
    
    }
    

    }
    }

    void Dialog_Login::on_pushButton_2_clicked()
    {
    Rellenar_Usuarios();
    }

    I need to see what is in the "id_usuario" field in the user table in the QTableWidget, only for testing
    But when i running it, the QTableWidget doesn't fill! What's wrong?? I'm Sorry my bad English!! Thanks


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    The first you should do is print what error you got if db.open() fails. Most likely, the driver wasn't loaded. If that's the case, what OS are you running ?



  • It does't print any error, I use windows 7, The driver is loaded, i compile it! and when i use the example project sqlbrowser , That shows me the data


  • Lifetime Qt Champion

    AFAIK, the sqlbrowser example uses a SQLite database which should always work. Did you modify it to use PostgreSQL ?

    In any case, you should also add a check that query.exec() successfully ran and again print the error if it failed.

    How do you know your plugin has been loaded ?



  • @JREMIROMERO

    What does

    query.lastError();
    

    return (right after query.exec() )?


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.