Results 1 to 7 of 7

Thread: ODBC driver error QODBCResult::data: column out of range

  1. #1
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default ODBC driver error QODBCResult::data: column out of range

    Greetings, I'm sending this query to a third party program database using an ODBC driver

    Qt Code:
    1. "SELECT table FROM Column WHERE DESCRIPCION = 'SOMETHING'"
    To copy to clipboard, switch view to plain text mode 

    my funtion should return a Stringlist with the values of column "o" where the value of column "k" match "pointer"

    Let's say, for example that my table is like
    Qt Code:
    1. code name lastname description
    To copy to clipboard, switch view to plain text mode 

    if i set o="0" it return the codes fine, but any value diferent that "0" return the error "QODBCResult::data: column # out of range".

    This is the same funtion that I use for a Postgres database but for some reason it doesn't work with the ODBC driver

    Qt Code:
    1. QStringList conecta2::leedb(QString table, int k, int o, QString pointer)
    2. {
    3. conectdbisam();
    4. QString line;
    5. QString line2;
    6. QStringList lista;
    7. int h;
    8. h=0;
    9. if(db.open())
    10. {
    11. QSqlQuery qry;
    12. if(qry.exec("SELECT * FROM "+table))
    13. {
    14. QSqlRecord rec = qry.record();
    15. columna = rec.fieldName(k);
    16. }
    17. if(qry.exec("SELECT "+columna+" FROM "+table+" WHERE "+columna+" = '"+pointer+"'"))
    18. {
    19. while(qry.next())
    20. {
    21. line2 =qry.value(o).toString().simplified();
    22. lista.append(line2);
    23. h++;
    24. if (h==500)
    25. break;
    26. }
    27. }
    28. else
    29. {
    30. qDebug() << "Error =" << db.lastError().text();
    31. }
    32. qry.clear();
    33. }
    34. else
    35. {
    36. qDebug() << "Error =" << db.lastError().text();
    37. }
    38. return QStringList(lista);
    39. }
    To copy to clipboard, switch view to plain text mode 

    Does anyone see the problem with this code, or maybe a work around?

    Thank in advance.

  2. #2
    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: ODBC driver error QODBCResult::data: column out of range

    In line 17 query result always have only one column. So that a value other than 0 for a variable o does not make sense.

  3. #3
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ODBC driver error QODBCResult::data: column out of range

    you are so right, just fix the problem by changing the sql comand to
    Qt Code:
    1. if(qry.exec("SELECT * FROM "+table+" WHERE "+columna+" = '"+pointer+"'"))
    To copy to clipboard, switch view to plain text mode 

    it works now, thank you!

  4. #4
    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: ODBC driver error QODBCResult::data: column out of range

    But this solution is worse. Why download anything from the database if you need only one column?

  5. #5
    Join Date
    Apr 2011
    Posts
    10
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: ODBC driver error QODBCResult::data: column out of range

    I just need the column that match a certain criteria, for example, Let's say my db is like this:

    Code name lastname description
    001 Andrew Gaven Ingenier
    002 Bill Doe CEO
    003 Jonh OConnor CEO
    .
    .

    I might just need the names of the CEO so my values should be pointer="CEO", k=3 (witch is my description column) and o=1 (the names column), ¿do you have a better idea?

  6. #6
    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: ODBC driver error QODBCResult::data: column out of range

    This really is quite an odd mechanism you are implementing. Typically you would know the names of the columns of interest without having to look them up using column indexes.

    Look up the column name of both columns and use those in your query.
    Qt Code:
    1. if(qry.exec( QString("SELECT * FROM %1 WHERE 1 = 0").arg(table) )) // condition stops row return but should allow column info
    2. {
    3. QSqlRecord rec = qry.record();
    4. columnk = rec.fieldName(k);
    5. columno = rec.fieldName(o);
    6. }
    7. if(qry.exec( QString("SELECT %1 FROM %2 WHERE %3 = '%4' ").arg(columno).arg(table).arg(columnk).arg(pointer) ))
    8. {
    9. while(qry.next())
    10. {
    11. line2 =qry.value(0).toString().simplified(); // That is a zero
    12. lista.append(line2);
    13. h++;
    14. if (h==500)
    15. break;
    16. }
    17. }
    To copy to clipboard, switch view to plain text mode 
    All assumes that o and k indexes are in range and that the table name and "pointer" value have been sanitised to avoid SQL injection issues.
    Last edited by ChrisW67; 16th January 2013 at 05:20.

  7. #7
    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: ODBC driver error QODBCResult::data: column out of range

    BTW the SQL standard guarantees a constant order of the columns in the query "SELECT * ...." ?

Similar Threads

  1. Deploying QT App using ODBC driver
    By AlphaWolfXV in forum Installation and Deployment
    Replies: 7
    Last Post: 5th December 2014, 14:19
  2. Slow ODBC driver or programming error?
    By TorAn in forum Qt Programming
    Replies: 9
    Last Post: 13th November 2011, 23:31
  3. source code for odbc driver or MySql driver in arm-embedded-linux
    By sattu in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 24th January 2011, 11:11
  4. remote ODBC data source
    By mchara in forum Qt Programming
    Replies: 7
    Last Post: 10th December 2007, 11:39
  5. Deploying debug Qt application - failed to load ODBC driver
    By sureshbabu in forum Installation and Deployment
    Replies: 1
    Last Post: 1st November 2007, 13:35

Tags for this Thread

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.