Results 1 to 8 of 8

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

  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 Why QSqlDatabase::open() returns open?

    I had a small problem and so, I was playing a lttle bit with the source.

    Can anobody tell my, why in the following code dbMandant.open() returns true, even if there is no host, no databaseName specified??

    Qt Code:
    1. QSqlDatabase dbMandant = QSqlDatabase::addDatabase( dbDriver, "mandant" );
    2.  
    3. //dbMandant.setHostName( dbHost );
    4. //dbMandant.setDatabaseName( dbName );
    5. dbMandant.setUserName( dbUser );
    6. dbMandant.setPassword( dbPasswd );
    7. //dbMandant.setPort( dbPort.toInt() );
    8.  
    9. qDebug() << dbMandant;
    10. qDebug() << dbMandant.open();
    To copy to clipboard, switch view to plain text mode 

    Here ist the output from qDebug:

    QSqlDatabase(driver=""QMYSQL"", database="""", host="""", port=-1, user=""root"", open=false)
    true

    The manual says:
    bool QSqlDatabase:pen ()
    opens the database connection using the current connection values. Returns true on success; otherwise returns false.

    I don't have a connection value for setDatabaseName, why open() returns true?

    Regards Guenther

  2. #2
    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
    I don't have a connection value for setDatabaseName, why open() returns true?
    because you don't need to specify a database name. You could connect to the server only as well, and that's the reason way you get true.

  3. The following user says thank you to Lykurg for this useful post:

    gboelter (27th August 2009)

  4. #3
    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?

    I found out a little bit more:

    In this code

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

    dbMandant.open() returns true.

    In this code

    Qt Code:
    1. dbMandant.setDatabaseName( "name of a non existing database" );
    To copy to clipboard, switch view to plain text mode 

    dbMandant.open() returns false, whats correct for me.

    I'm using Linux and Qt 4.5.2.

    Is that a bug or I'm only to stupid?

  5. #4
    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?

    Thanks for the fast reply!

    Quote Originally Posted by Lykurg View Post
    because you don't need to specify a database name. You could connect to the server only as well, and that's the reason way you get true.
    If this is true, the name of the function should be connected() and not open().

    But then I have another question? How can I make sure, that I'm really connected to "my" database and not only to the server?

  6. #5
    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
    But then I have another question? How can I make sure, that I'm really connected to "my" database and not only to the server?
    By using the setDatabaseName() function and set your database name; then call open() and you will see if you have opened correctly your database.

  7. #6
    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.

  8. #7
    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 

  9. #8
    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, 20:43
  2. Open file without run again the program
    By avis_phoenix in forum Qt Programming
    Replies: 1
    Last Post: 8th August 2009, 00:01
  3. Replies: 4
    Last Post: 3rd August 2009, 21:25
  4. QTextBrowser: Cannot open
    By veda in forum Qt Programming
    Replies: 3
    Last Post: 27th December 2007, 13:05
  5. Replies: 4
    Last Post: 8th July 2007, 15: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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.