Results 1 to 6 of 6

Thread: QSQLITE database can't exec queries

  1. #1
    Join Date
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Angry QSQLITE database can't exec queries

    Why this code is not working?
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE"); //I defined the "db" at header.
    2. db.setDatabaseName("../database");
    3. db.open();
    4. QSqlQuery query(db);
    5. query.exec("SELECT id FROM lesson");
    6. QMessageBox::about(0,"",query.value(0).toString());
    To copy to clipboard, switch view to plain text mode 

    code is running without any error but value which in message box is null. There is no result at query value.

  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: QSQLITE database can't exec queries

    Probably your relative path to the database is incorrect. See if QFile::exists("../database") returns true (check that before you open the database) and if that's indeed the database file you want.
    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
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QSQLITE database can't exec queries

    I checked that. I am not a newbie. db.open returned true, db.exists returned true also db.tables return my tables but it can't exec queries.

  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: QSQLITE database can't exec queries

    Ah, now I see the problem. You forgot to position the cursor on the first row of the result. Run query.first() before calling query.value().

    BTW. db.open() and db.exist() will always return true in case of SQLite (provided you have sufficient access rights to access the database file) so that's not a proof of anything.
    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
    Jan 2010
    Posts
    4
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: QSQLITE database can't exec queries

    Oh thanks, it is running.

  6. #6
    Join Date
    Dec 2009
    Location
    Brisbane
    Posts
    17
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE database can't exec queries

    Of note, query.next() also functions as query.first() if it's the first navigation call in the program flow.

Similar Threads

  1. QSqlite "database is locked"
    By flob in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 14:18
  2. Replies: 9
    Last Post: 19th November 2009, 09:18
  3. QTableView queries
    By prashant in forum Qt Programming
    Replies: 1
    Last Post: 3rd November 2009, 09:13
  4. QSqlQuery::exec: database not open
    By newtowindows in forum Qt Programming
    Replies: 8
    Last Post: 29th October 2009, 08:48
  5. QSqlite database lock + Delegate + QSqlQueryModel
    By NoRulez in forum Qt Programming
    Replies: 0
    Last Post: 13th October 2009, 11:52

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.