Results 1 to 7 of 7

Thread: Error executing SELECT query with QSQLITE

  1. #1
    Join Date
    May 2009
    Location
    Germany
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Error executing SELECT query with QSQLITE

    Hi all,

    I am experiencing trouble executing a sqlite SELECT query using the Qt library(4.5.0). Finally i broke it down to this small example app, which fails after executing the SELECT query without reporting an error, but query.isValid() returns false. Initializing the database and inserting values is fine and the sqlite command line utility is able to show the created entries, only Qt fails.

    Qt Code:
    1. #include <QtGui/QApplication>
    2. #include <QMessageBox>
    3. #include <QSqlDatabase>
    4. #include <QSqlQuery>
    5. #include <QSqlError>
    6.  
    7.  
    8. int main(int argc, char *argv[])
    9. {
    10. QApplication a(argc, argv);
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    12. db.setDatabaseName("test.db");
    13. db.open();
    14. QSqlQuery query = db.exec("CREATE TABLE vars "
    15. "(id integer primary key,"
    16. "name VARCHAR( 250 ),"
    17. "value VARCHAR( 250 ))");
    18. QSqlError err = db.lastError();
    19. if(!err.text().size() > 1)
    20. QMessageBox::information(0,"Error creating Database",err.text());
    21.  
    22. query = db.exec("INSERT INTO `vars` ( `name` , `value` ) VALUES ( 'testvar', 'testval');");
    23. err = db.lastError();
    24. if(!err.text().size() > 1)
    25. QMessageBox::information(0,"Error inserting to Database",err.text());
    26.  
    27. query = db.exec("SELECT * from vars;");
    28. err = db.lastError();
    29. if(!err.text().size() > 1)
    30. QMessageBox::information(0,"Error inserting to Database",err.text());
    31. if(!query.isValid()) {
    32. QMessageBox::information(0,"SELECT query not valid!",err.text());
    33. }
    34. if(!query.size()>0)
    35. QMessageBox::information(0,"SELECT result","At least one result read!");
    36. else
    37. QMessageBox::information(0,"SELECT failed","Select failed!");
    38.  
    39. return a.exec();
    40. }
    To copy to clipboard, switch view to plain text mode 

    Can someone tell my what I am missing?

    Thanks,
    garfield85

  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: Error executing SELECT query with QSQLITE

    What if you remove the semicolon from the query?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    May 2009
    Location
    Germany
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error executing SELECT query with QSQLITE

    i just tried it without semicolons and got the same error...am i missing something generally? is spent several hours on that issue today and don't know what's wrong

  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: Error executing SELECT query with QSQLITE

    Try putting the name of the table in quotes like you do for other queries.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  5. #5
    Join Date
    May 2009
    Location
    Germany
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error executing SELECT query with QSQLITE

    Hi!

    I also tried that but also with no effect. Did anybody get this to work? They only use the model/view thing in the Qt documentation but thats not what I need...

    thanks wysota for your suggestions! I'll wait some time if someone perhaps knows the trick or will have to use the sqlite library directly. But a simple select should not be an uncommon problem :-)

    garfield85

  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: Error executing SELECT query with QSQLITE

    The code is more or less correct. The problem is that you didn't read the docs carefully enough QSqlQuery::size() may return -1 if the number of rows in the result can't be determined. Just iterate the result with QSqlQuery::next().
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  7. #7
    Join Date
    May 2009
    Location
    Germany
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Error executing SELECT query with QSQLITE

    I was pretty sure i read this part of the documentation and also tried it with query.next(), but I gave it a shot once again and it worked!

    Thanks a lot for your patience, wysota!

    Greetings,
    garfield85

Similar Threads

  1. select query with like
    By ag.sitesh in forum Qt Programming
    Replies: 19
    Last Post: 14th April 2008, 05:28
  2. Unable to fetch data when searching through select query
    By sinha.ashish in forum Qt Programming
    Replies: 3
    Last Post: 10th April 2008, 14:06
  3. Select query using mysql for searching
    By sinha.ashish in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2008, 06:14

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.