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. [SOLVED] [QColumnView] Populate widget using for loops and placeholders in PostgreSQL queries

[SOLVED] [QColumnView] Populate widget using for loops and placeholders in PostgreSQL queries

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.2k 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.
  • V Offline
    V Offline
    valandil211
    wrote on last edited by
    #1

    Hello again!

    I have been trying to populate a QColumnView by using for loops and queries to my Postgres DB in conjunction. However, it seems that this code returns nothing.

    @void MainWindow::ENTModels()
    {
    // Initially hides the groupBox
    ui->ENT_groupBox->hide();

    // This piece of code populates the QColumnView with a nested
    // for loop. It does not work for some reason.
    entmodel = new QStandardItemModel;
    
    ENTfetch = new QSqlQuery(QSqlDatabase::database("*****"));
    ENTfetch->prepare("SELECT \"ENT_Nom\" FROM \"TB_Entretien\"");
    ENTfetch->exec();
    
    TESfetch = new QSqlQuery(QSqlDatabase::database("*****"));
    TESfetch->prepare("SELECT \"test\" FROM \"v_EntTest\" WHERE \"IndexEntretien\" = entretien"
                      "VALUES (?)");
    
    // Not the best solution, as it depends upon the fact that the value of
    // the PKEY is the same as the entretien number
    
    for (int entnumber = 0; entnumber < ENTfetch->size(); ++entnumber)
    {
        // Create Entretiens as first column
        ENTfetch->next();
        ent2test = new QStandardItem(ENTfetch->value(0).toString());
    
        TESfetch->bindValue(0, entnumber+1);
        TESfetch->exec&#40;&#41;;
    
        for (int tesnumber = 0; tesnumber < TESfetch->size(&#41;; ++tesnumber)
        {
            TESfetch->next();
            test2desc = new QStandardItem(TESfetch->value(0).toString());
            ent2test->appendRow(test2desc);
        }
    
    
        entmodel->appendRow(ent2test);
    }
    ui->ENT_entretiensColumnView->setModel(entmodel);
    

    }@

    The code seems to work when I do not use placeholders, but then it is pointless. Am I doing placeholders wrong? Can I bind values multiple times?

    Thanks for your answers!

    Joey Dumont

    1 Reply Last reply
    0
    • V Offline
      V Offline
      valandil211
      wrote on last edited by
      #2

      Is there a way to pass a C++ variable in the SQL statement? I just created a PostgreSQL function that fetches the data I need and takes an integer argument.

      In the code above, I want the argument of my function to be entnumber+1. Here's a code snippet (doesn't work):

      @ for (int entnumber = 0; entnumber < ENTfetch->size(); ++entnumber)
      {
      // Create Entretiens as first column

          ENTfetch->next();
          ent2test = new QStandardItem(ENTfetch->value(0).toString());
      
          TESfetch->prepare("SELECT f_ent2test(entnumber+1)");
          TESfetch->exec&#40;&#41;;
      
          for (int tesnumber = 0; tesnumber < TESfetch->size(); ++tesnumber)
          {
              TESfetch->next();
              test2desc = new QStandardItem(TESfetch->value(0).toString());
              ent2test->appendRow(test2desc);
          }
      
      
          entmodel->appendRow(ent2test);
      }
      

      @

      Now, this doesn't work. Is there a way to pass entnumber+1 to the SQL query?

      Thanks!

      Joey Dumont

      1 Reply Last reply
      0
      • H Offline
        H Offline
        HuXiKa
        wrote on last edited by
        #3

        Use something like this:

        @QSqlQuery qry;
        qry.prepare( "SELECT f_ent2test(:id)" );
        qry.bindValue( ":id", entnumber + 1 );@

        If you can find faults of spelling in the text above, you can keep them.

        1 Reply Last reply
        0
        • V Offline
          V Offline
          valandil211
          wrote on last edited by
          #4

          Thank you, @HuXiKa!

          I guess the VALUES clause is necessary only when dealing with multiple values?

          Joey Dumont

          1 Reply Last reply
          0

          • Login

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