Results 1 to 11 of 11

Thread: Database: How to use an external file?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Database: How to use an external file?

    Sorry, but what's the problem? What did you try so far? Just call QSqlDatabase::setDatabaseName() with a file name instead of ":memory:" and skip create table statements if they already exist.
    J-P Nurmi

  2. #2
    Join Date
    Jul 2007
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database: How to use an external file?

    I did. I got a query error that says the table does not exist? I did however put the open database statement in the .cpp file where I was using it. Does this matter? it looked like this:

    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("test.db");

    Do you see anything wrong here(missing ":" or something, I noticed the connection.h uses a ":" in front and back of memory)? My database open statement returns successfully, but the query error states that it cannot find a table I know is in there - The query statement I am using works from the command line.
    One note, this is the first reference to "test.db" I have in the project, meaning it is not included anywhere else, could this be something?

    goes2Bob

  3. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Database: How to use an external file?

    Hmm, are you 100% sure test.db lies in current working directory? What does
    Qt Code:
    1. #include <QtDebug>
    2. ...
    3. qDebug() << db.tables();
    To copy to clipboard, switch view to plain text mode 
    output?
    J-P Nurmi

  4. #4
    Join Date
    Jul 2007
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database: How to use an external file?

    I looked last night at where the db file was and it is in my workspace. By workspace, I mean that it is the /<proj name>/release directory (where I am running the executable). I also added the qDebug statement and it came back as "()" - I am guessing no tables, which explains why it cannot find the table. I was looking at the QSqlDatabase docs and found an example for an MS Access db statement which looked like this:

    Qt Code:
    1. db.setDatabaseName("DRIVER={Microsoft Access Driver (*.mdb)};FIL={MS Access};DBQ=myaccessfile.mdb");
    To copy to clipboard, switch view to plain text mode 

    does there have to be anything in the "setDatabaseName" statement telling the application that the file is external? Remember as I stated in previous post that I have not included the external file in any way. The first time the application sees the filename is in the setDatabaseName statement. Should it be included in the .pro file? or as an extern?
    Thanks in advance again,
    goes2Bob

  5. #5
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Database: How to use an external file?

    No, that's MS Access specific - SQLite takes the file name. I still suspect it's a problem with relative paths and that the current working directory is not the same where .db file is located.

    Since you have test.db next to application executable, you could try:
    Qt Code:
    1. QDir dir(QApplication::applicationDirPath());
    2. db.setDatabaseName(dir.filePath("test.db"));
    3. qDebug() << db.tables();
    To copy to clipboard, switch view to plain text mode 
    J-P Nurmi

  6. #6
    Join Date
    Nov 2007
    Posts
    89
    Thanked 21 Times in 18 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database: How to use an external file?

    You can also check if database exists using QFile::exists or QFileInfo::exists.

  7. #7
    Join Date
    Jul 2007
    Posts
    11
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Database: How to use an external file?

    Success ! ! adding the "setDatabaseName(dir.filePath("test.db")); " allowed the app to find the file - and queries work just as they should.
    This begs the question: why was that path direction necessary?

    I am using the Eclipse IDE, Qt 4.2, on WinXP.

    Thanks for your patience,
    goes2Bob

  8. #8
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Database: How to use an external file?

    Consider the difference between:
    C:\myapp> release\myapp.exe
    and
    C:\myapp\release> myapp.exe
    When launching the application in first way, current working directory is "C:\myapp" whereas in the latter way it's "C:\myapp\release". Windows IDEs tend to launch the application from project dir, not from release or debug subdirectory.
    J-P Nurmi

Similar Threads

  1. Replies: 16
    Last Post: 23rd May 2008, 10:12
  2. Set up the Qt4.3.2 with Visual Studio 2005
    By lamoda in forum Installation and Deployment
    Replies: 6
    Last Post: 30th January 2008, 06:51
  3. EXE File problem
    By triperzz in forum Installation and Deployment
    Replies: 8
    Last Post: 18th January 2008, 19:00
  4. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  5. Link Errors
    By magikalpnoi in forum Qt Programming
    Replies: 5
    Last Post: 25th September 2006, 22:04

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.