Results 1 to 15 of 15

Thread: column out of range problem

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default 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.
    Qt Code:
    1. void Sales::on_comboBoxName_currentTextChanged(const QString &arg1)
    2. {
    3. QString sql = "select productID, Rate, Stock from tableProductRecords where productname Like '%"+arg1+ "%';";
    4. query->prepare(sql);
    5. if(query->exec(sql))
    6. {
    7. if(query->next())
    8. {
    9. ui->lineEditProductNumber->setText(query->value(0).toString().trimmed());
    10. qDebug() << "ui->lineEditProductNumber->text().trimmed();" << ui->lineEditProductNumber->text().trimmed();
    11. QString str1 = query->value(1).toString().trimmed();
    12.  
    13. ui->lineEditRate->setText(str1); // here is error of column out of range
    14. qDebug() <<" ui->lineEditRate->text().trimmed(); " << ui->lineEditRate->text().trimmed();
    15. QString str2 = query->value(2).toString().trimmed();
    16. ui->lineEditStock->setText(str2);// here is error of column out of range
    17. qDebug() <<"ui->lineEditStock->text().trimmed();" << ui->lineEditStock->text().trimmed();
    18. }
    19. }
    20. }
    To copy to clipboard, switch view to plain text mode 
    output :-
    Qt Code:
    1. ui->lineEditProductNumber->text().trimmed(); "1"
    2.  
    3. QMYSQLResult::data: column 1 out of range
    4.  
    5. ui->lineEditRate->text().trimmed(); ""
    6.  
    7. QMYSQLResult::data: column 2 out of range
    8. ui->lineEditStock->text().trimmed(); ""
    9.  
    10. ui->lineEditProductNumber->text().trimmed(); "1"
    11.  
    12. ui->lineEditRate->text().trimmed(); "7000"
    13. ui->lineEditStock->text().trimmed(); "4"
    To copy to clipboard, switch view to plain text mode 
    how do I get rid of "column out of range".

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: column out of range problem

    ui->lineEditProductNumber->setText(query->value(0).toString().trimmed());
    It always amazes me that programmers can write this kind of code, with absolutely no error checking, assuming that every pointer points to a valid instance and that every variable will contain or method call will result in a valid piece of data.

    Don't they teach "defensive programming" any more? Even simple error checking? Or does everyone do like Boeing, and let a couple of planes full of people crash into the ground before they fix the bugs in the software?

    how do I get rid of "column out of range".
    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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    I tried like this :-

    bool str1 = query->isValid();
    bool flag = query->isActive();

    QMessageBox::information(this, "on_comboBoxName_currentTextChanged", " flag : " + QString(flag) + " str1 : " + str1);
    but gives nothing
    column out of range _ pic.jpgcolumn out of range _ pic.jpg

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: column out of range problem

    Why do you think checking the status of the entire query will help in determining whether the contents of a specific column is valid or not? If you are getting these errors it means that 1) your database is corrupt or 2) there is no data in the columns you are asking for (those columns are NULL). Why don't you open your database in MySQL Workbench and examine what is really in the columns you are trying to retrieve?
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: column out of range problem

    The first observation would make is that the error messages cannot possibly be coming from the lines indicated in the original code listing. The error plainly comes from the underlying SQL driver and the indicated lines are not accessing anything that would touch that. The messages originate with the line before in both cases.

    The error message implies that the record you are looking at does not have field at index 1 or 2. This is not the same as these fields existing but being NULL or some type that cannot be converted to a QString. Rather it implies that the SQL the query object is returning rows from returns only a single column (since index 0 exists). The code you posted does not seem to match the result you claim if taken in isolation.

    Where is the "query" variable declared? It is not local to this function (perhaps a member variable?), which opens the possibility that "query" is changing as result of other events outside this function. For example, is line 9 triggering a slot that modifies "query"?

    qDebug value 0, 1, and 2 before line 9 to see what you have.
    "We can't solve problems by using the same kind of thinking we used when we created them." -- Einstein
    If you are posting code then please use [code] [/code] tags around it - makes addressing the problem easier.

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    Before line 9 put this code :
    Qt Code:
    1. QSqlRecord rec = query.record();
    2. for(int i = 0; i < rec.size(); i++)
    3. qDebug() << rec.fieldName(i);
    To copy to clipboard, switch view to plain text mode 
    You will see how many columns the record has and what they are called.

  7. #7
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    your code I put it in above line 9.
    QSqlRecord rec = query.record();
    for(int i = 0; i < rec.count(); i++)// size is not member of QSqlRecord
    qDebug() << rec.fieldName(i);
    and having this output :-
    Qt Code:
    1. Starting /opt/projects/Qt/cbs_soft/build-cbs-Desktop_Qt_5_7_0_GCC_64bit-Debug/cbs...
    2. "productID"
    3. "Rate"
    4. "Stock"
    5. QMYSQLResult::data: column 1 out of range
    6. QMYSQLResult::data: column 2 out of range
    To copy to clipboard, switch view to plain text mode 
    Last edited by rahulvishwakarma; 10th August 2020 at 09:41.

  8. #8
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: column out of range problem

    Yes, and what have you done with that information? After line 9 what does that Lesiok's debug code return?

  9. #9
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    i posted output in on above post. I didn't get your point by "what have you done with that information?"
    Last edited by rahulvishwakarma; 11th August 2020 at 13:36.

  10. #10
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    And what happens when you replace indexes with names, that is:
    Qt Code:
    1. ui->lineEditProductNumber->setText(query->value("productID").toString().trimmed());
    To copy to clipboard, switch view to plain text mode 
    etc.

  11. #11
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    this outputs as following :-
    Qt Code:
    1. QSqlQuery::value: unknown field name 'Stock'
    2. ui->lineEditStock->setText(query->value("Stock").toString().trimmed()); : ""
    To copy to clipboard, switch view to plain text mode 


    and
    Qt Code:
    1. ui->lineEditProductNumber->setText(query->value("productId").toString().trimmed());
    2. ui->lineEditProductNumber->text : "1"
    To copy to clipboard, switch view to plain text mode 
    Last edited by rahulvishwakarma; 12th August 2020 at 17:05.

  12. #12
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: column out of range problem

    QSqlQuery::value: unknown field name 'Stock'
    HINT, HINT: Your query is not returning you the record you think it is. The table you are querying has a field named "productId", but it doesn't have fields named "Rate" or "Stock" and so the record your query returns doesn't have more than one column.

    As I said earlier, open your database using MySQL Workbench and actually look at the structure of the database and the tables in it. Then look at the query you are constructing in your code and see if it matches what you see in the Workbench.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  13. #13
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: column out of range problem

    Quote Originally Posted by d_stranz View Post
    HINT, HINT: Your query is not returning you the record you think it is. The table you are querying has a field named "productId", but it doesn't have fields named "Rate" or "Stock" and so the record your query returns doesn't have more than one column.

    As I said earlier, open your database using MySQL Workbench and actually look at the structure of the database and the tables in it. Then look at the query you are constructing in your code and see if it matches what you see in the Workbench.
    It is very strange to me. If the table has no columns, query->exec() should return false. The list of the record columns showed 3 names.
    Either the code shown is different from the real code or MySQL is behaving strangely.

  14. #14
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: column out of range problem

    It is very strange to me. If the table has no columns, query->exec() should return false. The list of the record columns showed 3 names.
    Either the code shown is different from the real code or MySQL is behaving strangely.
    That was my thought too, but the OP claims everything is working except for the missing columns. There is no code to show how "query" is being created and used prior to the code snippet given in the first post..
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  15. #15
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: column out of range problem

    Quote Originally Posted by rahulvishwakarma View Post
    i posted output in on above post.
    No, you posted the output before line 9 as Lesiok asked. I asked for the output after line 9.

    The information from your output seems to be what is expected at that point in your program flow, but we know that later in the procedure the record is not what is expected. What happens in between, and how can it happen? (I also asked some questions that hint at how something unexpected might happen.)

    I didn't get your point by "what have you done with that information?"
    I guess my point is that I (we) would like to know what you have tried to debug the problem.

Similar Threads

  1. QMYSQLResult::data: column 1 out of range
    By rahulvishwakarma in forum Qt Programming
    Replies: 1
    Last Post: 6th June 2020, 05:18
  2. ODBC driver error QODBCResult::data: column out of range
    By Andrewgaven in forum Qt Programming
    Replies: 6
    Last Post: 16th January 2013, 10:30
  3. Index out of range problem for a xml file
    By Niamita in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2011, 08:11
  4. Qt Creator Problem with column indenting
    By pauljurczak in forum Qt Tools
    Replies: 3
    Last Post: 30th March 2011, 16:56
  5. QList index out of range problem
    By MarkoSan in forum Qt Programming
    Replies: 2
    Last Post: 26th March 2008, 09:40

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.