PDA

View Full Version : Why QSqlDatabase::open() returns open?



gboelter
27th August 2009, 17:14
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??



QSqlDatabase dbMandant = QSqlDatabase::addDatabase( dbDriver, "mandant" );

//dbMandant.setHostName( dbHost );
//dbMandant.setDatabaseName( dbName );
dbMandant.setUserName( dbUser );
dbMandant.setPassword( dbPasswd );
//dbMandant.setPort( dbPort.toInt() );

qDebug() << dbMandant;
qDebug() << dbMandant.open();


Here ist the output from qDebug:

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

The manual says:
bool QSqlDatabase::open ()
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

Lykurg
27th August 2009, 17:33
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.

gboelter
27th August 2009, 17:33
I found out a little bit more:

In this code



dbMandant.setDatabaseName( "" );


dbMandant.open() returns true.

In this code



dbMandant.setDatabaseName( "name of a non existing database" );


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? :confused:

gboelter
27th August 2009, 17:39
Thanks for the fast reply!


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?

Lykurg
27th August 2009, 17:53
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.

gboelter
27th August 2009, 18:10
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.

vfernandez
27th August 2009, 18:17
Then you can just make sure dbName is not empty.


QSqlDatabase db = QSqlDatabase::addDatabase(...);

QString dbName = getDatabase();
if(dbName.isEmpty()) {
emit error();
return;
}

db.setDatabaseName(dbName);
db.connect();

Lykurg
27th August 2009, 18:52
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.