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. Problems querying on Postgres

Problems querying on Postgres

Scheduled Pinned Locked Moved Unsolved General and Desktop
5 Posts 3 Posters 1.1k Views
  • 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.
  • J Offline
    J Offline
    JREMIROMERO
    wrote on 6 Mar 2016, 21:02 last edited by JREMIROMERO 3 Jun 2016, 21:15
    #1

    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

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 6 Mar 2016, 22:26 last edited by
      #2

      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 ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • J Offline
        J Offline
        JREMIROMERO
        wrote on 6 Mar 2016, 23:31 last edited by
        #3

        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

        T 1 Reply Last reply 7 Mar 2016, 08:18
        0
        • S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 7 Mar 2016, 07:37 last edited by
          #4

          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 ?

          Interested in AI ? www.idiap.ch
          Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • J JREMIROMERO
            6 Mar 2016, 23:31

            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

            T Offline
            T Offline
            the_
            wrote on 7 Mar 2016, 08:18 last edited by
            #5

            @JREMIROMERO

            What does

            query.lastError();
            

            return (right after query.exec() )?

            -- No support in PM --

            1 Reply Last reply
            0

            1/5

            6 Mar 2016, 21:02

            • Login

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