Results 1 to 8 of 8

Thread: Why QSqlDatabase::open() returns open?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2008
    Location
    Davao City, Philippines
    Posts
    77
    Thanks
    16
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Why QSqlDatabase::open() returns open?

    Quote Originally Posted by Lykurg View Post
    By using the setDatabaseName() function and set your database name; then call open() and you will see if you have opened correctly your database.
    That's what I'm doing normally. But in this case I get the name of the dataBase from another database using a getter called getDatabase(). And with open() alone there is a high risk, that getDatabase() will return an empty string but open() will return true.

    Ok, I can change my getter, but have expected, that Qt4 can do that alone.

  2. #2
    Join Date
    Jan 2006
    Location
    Innsbruck, Austria
    Posts
    62
    Thanks
    10
    Thanked 7 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: Why QSqlDatabase::open() returns open?

    Then you can just make sure dbName is not empty.

    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase(...);
    2.  
    3. QString dbName = getDatabase();
    4. if(dbName.isEmpty()) {
    5. emit error();
    6. return;
    7. }
    8.  
    9. db.setDatabaseName(dbName);
    10. db.connect();
    To copy to clipboard, switch view to plain text mode 

  3. #3
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Why QSqlDatabase::open() returns open?

    Quote Originally Posted by gboelter View Post
    Ok, I can change my getter, but have expected, that Qt4 can do that alone.
    But if Qt would return false if the database is empty, then you couldn't connect only to the server with Qt. So, Qt works as one expect it. And for your case simply check re returned value of your function if it is empty as vfernandez has suggested.

Similar Threads

  1. Replies: 3
    Last Post: 7th October 2015, 19:43
  2. Open file without run again the program
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 7th August 2009, 23:01
  3. Replies: 4
    Last Post: 3rd August 2009, 20:25
  4. QTextBrowser: Cannot open
    By veda in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2007, 12:05
  5. Replies: 4
    Last Post: 8th July 2007, 14:26

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.