Results 1 to 4 of 4

Thread: Empty SQlite Database

  1. #1
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Empty SQlite Database

    I have stored my database in my project folder and made the necessary changes to my .pro file. I currently can open and access the database but it claims that there are no tables. The database, when opened outside of QT, has multiple tables and the queries execute properly. In QT I do the following to run a query:

    Qt Code:
    1. qDebug() << "Open?" << db.isOpen() << endl;
    2. qDebug() << "isVALID" << db.isValid() << endl;
    3. qDebug() << "SIZE" << db.tables().size() << endl;
    4.  
    5. QSqlQuery query("SELECT * FROM shifts", db);
    6. if(!query.exec())
    7. {
    8. qDebug() << query.lastError().text();
    9. qDebug() << "ERROR" << endl;
    10. return;
    11. }
    12. while(query.next())
    13. {
    14.  
    15. QString name = query.value(1).toString();
    16.  
    17. qDebug() << name << endl;
    18.  
    19. }
    To copy to clipboard, switch view to plain text mode 

    My output says the database is open, valid, and size is 0. Also, the variable db is of type QSqlDatabase and is established in the constructor of my class. Why does it assume the database is empty? Any suggestions on what is going wrong?

  2. #2
    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: Empty SQlite Database

    You are not opening the database you think you are. Sqlite will happily 'open' a non-existent or zero-byte file and treat it as a database with no tables. Execute "PRAGMA database_list;" as Sql to see exactly what file is open by the Qt program (third column). If there is no file name then you have a ":memory:" database.

  3. #3
    Join Date
    Apr 2013
    Posts
    2
    Thanks
    1
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: Empty SQlite Database

    Thank you! You were right! Although I put my sqlite database in my project folder and added it accordingly to my .pro file, the file path was still wrong and thus it was opening a different database than I thought. However I now have the file path hardcoded, which is a poor solution as I am looking to deploy this application (obviously everyone's file paths will be different). Any suggestion on where to put the database so it can be loaded once the application starts up? It does not appear to work if I add it as a "Resource"

  4. #4
    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: Empty SQlite Database

    If it is read-only then put it next to the program executable and use QCoreApplication::applicationDirPath(). For development ONLY you can place a writable database here too. If it is to be read-write then one of the locations available through QDesktopServices::storageLocation() (Qt4) or QStandardPaths (Qt5) is where it should be ultimately.

  5. The following user says thank you to ChrisW67 for this useful post:

    papasmurf1212 (15th April 2013)

Similar Threads

  1. Sqlite Database
    By sabbu in forum Qt Programming
    Replies: 5
    Last Post: 16th May 2011, 13:07
  2. PyQt4: My database displays empty cells
    By Danny Hatt in forum Qt Programming
    Replies: 1
    Last Post: 10th June 2010, 14:23
  3. Use QT to set password for SQLITE database. help...!
    By Kevin Hoang in forum Qt Programming
    Replies: 1
    Last Post: 11th March 2010, 07:28
  4. QTableView from SQLite, empty!
    By grellsworth in forum Qt Programming
    Replies: 9
    Last Post: 3rd July 2007, 20:32
  5. [QT4][SQLITE] Database and query
    By agent007se in forum Newbie
    Replies: 10
    Last Post: 12th July 2006, 22:16

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.