Page 1 of 2 12 LastLast
Results 1 to 20 of 25

Thread: QSqlQuery::isValid returns false

  1. #1
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Question QSqlQuery::isValid returns false

    hello,
    i am accessing a ms access database and i have the folowing problem.

    if(query->isSelect())
    QMessageBox::warning(this, "title", "select true"); //always returns true
    query->last();
    if(query->isValid())
    QMessageBox::warning(this, "title", "true"); // always return false

    when i execute last() function it does not return the last record and the query position is set to an invalid position.
    why when i execute the last() function it does not return the required record?
    and why it points to an invalid position?
    thank you

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    What does QSqlQuery::isActive() return?

  3. #3
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    What does QSqlQuery::isActive() return?
    i tried it before and after the QSqlQuery::last()
    and it always returns true

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    Can you provide the whole block of code used to operate on the query?

  5. #5
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    that is the code i use
    Qt Code:
    1. #include "shamelareader.h"
    2.  
    3. shamelaReader::shamelaReader(QWidget *parent)
    4. : QMainWindow(parent)
    5. {
    6. mainText = new QTextEdit(this);
    7. setCentralWidget(mainText);
    8.  
    9. db= QSqlDatabase::addDatabase("QODBC");
    10. db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=c:\\book.mdb");
    11. db.open();
    12.  
    13.  
    14. createActions();
    15. createToolBar();
    16.  
    17. query = new QSqlQuery();
    18. query->exec("SELECT name FROM employee");
    19. query->next();
    20. mainText->setText(query->value(0).toString());
    21.  
    22. }
    23. void shamelaReader::createActions()
    24. {
    25. nextAct = new QAction(tr("&Next"),this);
    26. connect(nextAct, SIGNAL(triggered()), this, SLOT(next()));
    27.  
    28. prevAct = new QAction(tr("&previous"),this);
    29. connect(prevAct, SIGNAL(triggered()), this, SLOT(prev()));
    30.  
    31. firstAct = new QAction(tr("first"), this);
    32. connect(firstAct, SIGNAL(triggered()), this, SLOT(first()));
    33.  
    34. lastAct = new QAction(tr("last"), this);
    35. connect(lastAct, SIGNAL(triggered()), this, SLOT(last()));
    36.  
    37.  
    38. }
    39.  
    40. void shamelaReader::createToolBar()
    41. {
    42. toolBar = addToolBar(tr("Navigation"));
    43. toolBar->addAction(firstAct);
    44. toolBar->addAction(nextAct);
    45. toolBar->addAction(prevAct);
    46. toolBar->addAction(lastAct);
    47. }
    48.  
    49. void shamelaReader::next()
    50. {
    51. query->next();
    52. mainText->setText(query->value(0).toString());
    53. }
    54.  
    55. void shamelaReader::prev()
    56. {
    57. query->previous();
    58. mainText->setText(query->value(0).toString());
    59. }
    60.  
    61. void shamelaReader::first()
    62. {
    63. query->first();
    64. mainText->setText(query->value(0).toString());
    65. }
    66.  
    67. void shamelaReader::last()
    68. {
    69.  
    70. query->last();
    71. if(query->isActive())
    72. QMessageBox::warning(this, "title", "Active true");
    73.  
    74. if(query->isValid())
    75. QMessageBox::warning(this, "title", "true");
    76.  
    77. mainText->setText(query->value(0).toString());
    78. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by wysota; 13th March 2008 at 15:15. Reason: Changed [qtclass] to [code]

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    What does query->last() return? Maybe the driver only supports sequential access? Have you verified that?

  7. #7
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    query->last()
    return true
    Maybe the driver only supports sequential access? Have you verified that?
    how can i know that the driver does not support sequential access

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    if last() returns true, then it means the query has been positioned correctly.

    Qt Code:
    1. if(query->last(){
    2. QSqlRecord rec = query->record();
    3. qDebug("%d", rec.count());
    4. }
    To copy to clipboard, switch view to plain text mode 

    What does this output? Remember to have console enabled for your project or else you won't see anything.

  9. #9
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    excuse me but i dont know how to enable the console.
    i am using visual studio 2005.
    i hope, i am not wasting your time.
    but i believe if i want to learn some thing i must start my project directly as sayed in Qt documentation .but on the road there are missed things.
    thank you

  10. #10
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    If you're using a commercial licence then access your project options using the integration plugin and it should be there somewhere. If not, run your application from within VS debugger and it should display the message. As an alternative, prepare a small qmake project and include a CONFIG+=console line in it.

  11. #11
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    i made as you said and i get the following output:
    1
    QSqlQuery::value: not positioned on a valid record

  12. #12
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    How many records does the table hold?

  13. #13
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    i tried it with two databases
    one of them contains three records
    and the other one contains 90 records.

  14. #14
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    Could you prepare a minimal compilable example reproducing the problem?

  15. #15
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    here, it is the source code .
    http://www.filesend.net/download.php...df51c62f649b7c

    please dont forget to put the sample database in the ( c:\ ) directory.
    the database are contained in the Zip file and also the *.pro file.

  16. #16
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    i tried the same code with Sqlite database and the program works fine.
    but why does not it work with MS access?

  17. #17
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    As I (think I) already said, maybe Access only supports sequential access to data.

  18. #18
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    do you mean that i can not deal with access databases with Qt?

  19. #19
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlQuery::isValid returns false

    No, I mean Access databases can't jump across the result set regardless if you use Qt or not. If you want to reach the last entry, you have to iterate each record one by one until you reach the end.

  20. #20
    Join Date
    Feb 2008
    Posts
    154
    Thanks
    12
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::isValid returns false

    but i have the program that deal with access database which is built using visual basic
    and this program go through the database easy it make last , first, next and previous
    and i want to rebuild this program using c++. it means that the database support non sequential access
    as well as sequential.

Similar Threads

  1. Replies: 4
    Last Post: 8th July 2007, 14:26
  2. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 20:01

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.