Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

column out of range problem



  • i am building small project, in Qt 5.7(on client) and mysql( on server). then in following program I stuck in "QMYSQLResult::data: column 1 out of range" problem. please help how to get rid of this.

    void Sales::on_comboBoxName_currentTextChanged(const QString &arg1)
    {
        QString sql = "select productID, Rate, Stock from tableProductRecords where productname Like '%"+arg1+ "%';";
        query->prepare(sql);
        if(query->exec(sql))
        {
            if(query->next())
            {
                ui->lineEditProductNumber->setText(query->value(0).toString().trimmed());
                qDebug() << "ui->lineEditProductNumber->text().trimmed();" << ui->lineEditProductNumber->text().trimmed();
                QString str1 = query->value(1).toString().trimmed();
    
                ui->lineEditRate->setText(str1); // here is error of column out of range
                qDebug() <<" ui->lineEditRate->text().trimmed(); " << ui->lineEditRate->text().trimmed();
                QString str2 = query->value(2).toString().trimmed();
                ui->lineEditStock->setText(str2);// here is error of column out of range
                qDebug() <<"ui->lineEditStock->text().trimmed();" <<   ui->lineEditStock->text().trimmed();
            }
        }
    }
    

    output :-

    ui->lineEditProductNumber->text().trimmed(); "1"
    
    QMYSQLResult::data: column 1 out of range
    
     ui->lineEditRate->text().trimmed();  ""
    
    QMYSQLResult::data: column 2 out of range
     ui->lineEditStock->text().trimmed(); ""
    
    ui->lineEditProductNumber->text().trimmed(); "1"
    
     ui->lineEditRate->text().trimmed();  "7000"
     ui->lineEditStock->text().trimmed(); "4"
    

    how do I get rid of "column out of range".



  • @rahulvishwakarma said in column out of range problem:

    QMYSQLResult::data: column 1 out of range

    You asked this question at https://www.qtcentre.org/threads/71156-column-out-of-range-problem a week ago. I'm afraid the answer is the same as there, you really need to do error checking and you are not:

    QSqlQuery::isNull(), QSqlQuery::isValid(), QVariant::isValid(), QVariant::isNull(), QVariant::canConvert() are just a few of the methods available that might result in more reliable code and fewer error messages.

    At a guess, the Rate and Stock returned from your row have value NULL? Have you looked at what you have actually put in the row in your database?



  • I tried as you said and getting nothing as follows:-

    bool flag = query->isActive();
    QMessageBox::information(this, "on_comboBoxName_currentTextChanged", " str1 : "  + flag);
    

    it prints nothing


  • Lifetime Qt Champion

    Hi,

    You have two if statements that checks both if the execution was successful as well as the next call. Put an else close for each of them and there print the error you may have.


Log in to reply