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



  • 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!



  • 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!



  • Use something like this:

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



  • Thank you, @HuXiKa!

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


Log in to reply
 

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