Results 1 to 10 of 10

Thread: Unable to work with database

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Unable to work with database

    I've been looking into how to add an existing database into a project, however I've not been able to figure out exactly how to do it and it's not really clearly documented in the tutorial. Here is what I presently have in the .pro file:

    Qt Code:
    1. LIBS += C:\Users\Administrator\Desktop\sqlite3.dll
    2.  
    3. addFiles.sources = test.db
    4. addFiles.path = .
    To copy to clipboard, switch view to plain text mode 

    How do I specify an absolute path? Is this done in

    Qt Code:
    1. db.setDatabaseName("testdb");
    To copy to clipboard, switch view to plain text mode 
    ?

    I can't find any obvious function in:

    http://qt-project.org/doc/qt-4.8/qsqldatabase.html

    That allows one to specify the path of the .db file.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Unable to work with database

    Connecting to a database has been absolutely done to death in these forums.
    Here is what I presently have in the .pro file
    I have no idea where you got this from. Remove the lines you gave us and ensure that you do have:
    Qt Code:
    1. QT += sql
    To copy to clipboard, switch view to plain text mode 
    How do I specify an absolute path?
    For Sqlite:
    Qt Code:
    1. db.setDatabaseName("/some/full/path/to/the/test.db");
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Unable to work with database

    Thanks Chris, you were right and I didn't even process what you wrote about my query being questionable at best, looks like it's all been working all along. Many thanks and silly me .

  4. #4
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Unable to work with database

    Qt Code:
    1. QString strDBName = "D:\path to .db file where the .exe is data.db";
    2. db.setDatabaseName(strDBName);
    3. bool dbOpen = db.open(strDBName, NULL);
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. query.exec("SELECT Column1, Column2, Column3, Column4, Column5, Column6, FROM tblTable WHERE (Column4='EMPTY')"); //
    2. qDebug() << query.lastError().text(); //if error executing SQL statement occurs, get error
    3.  
    4. //Selected rows based on condition
    5. while(query.next())
    6. {
    7. qDebug() << "inside while loop";
    8. int iRow = query.value(0).toInt();
    9. int iColumn = query.value(1).toInt();
    10. qDebug() << "Row = " << iRow << "Column = " << iColumn;
    11. }
    To copy to clipboard, switch view to plain text mode 

    I've got this code, and unfortunately it's not producing any results. The first qDebug with lastError(), prints " ". According to the documentation (Returns error information about the last error (if any) that occurred with this query.), not sure if the empty brackets indeed correspond to the fact that no error occured or whether we're supposed to get no output at all if no error occured but I'm assuming that no error occured.

    The second qDebug statement inside the 'while' loop nevergets executed so not sure why the program never enters the 'while' loop.

    I've tried PRAGMA:

    Qt Code:
    1. query.exec("PRAGMA database_list");
    2. //qDebug() << query.lastError().text(); //if error executing SQL statement occurs, get error
    3.  
    4. //List of currently available databases
    5. while(query.next())
    6. {
    7. int iDatabaseConnection = query.value(2).toInt();
    8. QString sDatabaseSecondColumn = query.value(2).toString();
    9. QString sDatabaseFileName = query.value(2).toString();
    10. qDebug() << "Database connection " << iDatabaseConnection << "Database file name = " << sDatabaseFileName << "Second column = " << sDatabaseSecondColumn;
    11. }
    To copy to clipboard, switch view to plain text mode 

    and the result was:

    Database connection 0 Database file name = "D:\path to .db file where the .exe is data.db" Second column = "D:\path to .db file where the .exe is data.db"

    So the PRAGMA query enters the 'while' loop but the other does not.

  5. #5
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Unable to work with database

    I edited the code a litte:
    Qt Code:
    1. qDebug() << "Database sequence " << iDatabaseSequence << "Database name = " << sDatabaseName << "File associated = " << sDatabaseFileName;
    To copy to clipboard, switch view to plain text mode 

    Now it prints out:

    Database sequence 0 Database name = "main" File associated = "D:\path to .db file where the .exe is data.db"

  6. #6
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Wiki edits
    17

    Default Re: Unable to work with database

    Your query as posted is not valid SQL and should be returning an error.

    If the query was corrected in the obvious way and returns no rows, i.e. the while() loop is never entered, that would be because there are no rows in that table with 'EMPTY' in column4. This is correct behaviour.

  7. #7
    Join Date
    Jun 2011
    Posts
    203
    Qt products
    Qt4
    Platforms
    MacOS X Windows
    Thanks
    7
    Thanked 4 Times in 3 Posts

    Default Re: Unable to work with database

    Turns out, everything is working fine, I think my database is corrupt (or I stuffed up access to sqlite conversion), if I search for WHERE column4 = 'SOMETHING', it works, and indeed, if I just pull all the data that it can without the where statement, it only pulls the the SOMETHINGs, so not quite sure where the database got corrupted but at least it pretty much solved.

Similar Threads

  1. Replies: 0
    Last Post: 28th June 2013, 11:52
  2. Mysql unknown database, QMYSQL unable to connect
    By lixo1 in forum Qt Programming
    Replies: 3
    Last Post: 14th July 2010, 21:39
  3. Replies: 9
    Last Post: 20th May 2010, 09:55
  4. ODBC Oracle. Unable to connect to database
    By egil in forum Qt Programming
    Replies: 2
    Last Post: 17th October 2008, 13:46
  5. which ide/editor/database work best with qt4?
    By kkool84 in forum Installation and Deployment
    Replies: 17
    Last Post: 24th August 2006, 06:45

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
  •  
Qt is a trademark of The Qt Company.