Qt-interest Archive, April 2007
QSqlDatabase::database()
Message 1 in thread
Assistant say about QSqlDatabase::addDatabase:
If connectionName is not specified, the newly added database connection
becomes the default database connection for the application, and subsequent
calls to database() without a database name parameter will return a
reference to it.
So, why next code give me warning?
-----------------------------------------------
#include <QApplication>
#include <QMessageBox>
#include <QSqlDatabase>
void myMessageOutput(QtMsgType type, const char *msg) {
switch (type) {
case QtDebugMsg:
QMessageBox::information(NULL, "Debug", msg);
break;
case QtWarningMsg:
QMessageBox::warning(NULL, "Warning", msg);
break;
case QtCriticalMsg:
QMessageBox::critical(NULL, "Critical", msg);
break;
case QtFatalMsg:
QMessageBox::critical(NULL, "Fatal", msg);
abort();
}
}
int main(int argc, char *argv[])
{
qInstallMsgHandler(myMessageOutput);
QApplication app(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
/// next line gives warning: QSqlDatabasePrivate::database: unable to open
database:
/// but actualy set up database name!
QSqlDatabase::database().setDatabaseName("aaa.dat");
return 0;
}
----------------------------------
--
[ signature omitted ]
Message 2 in thread
Hi,
> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
Did the above call to QSqlDatabase::addDatabase() succeed?
> /// next line gives warning: QSqlDatabasePrivate::database: unable to open
> database:
> /// but actualy set up database name!
> QSqlDatabase::database().setDatabaseName("aaa.dat");
--
[ signature omitted ]
Message 3 in thread
Yes, it did.
Even code like this works perfectly fine:
----------
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase::database().setDatabaseName("aaa.dat");
QSqlDatabase::database().open();
QSqlQuery qry;
qry.exec("create table t(id integer);");
QSqlDatabase::database().close();
----------
But the very first attempt to call QSqlDatabase::database() - gives a
warning. Subsequent calls go without any problem.
"Dimitri" <dimitri@xxxxxxxxxxxxx> wrote in message
news:f062dc$h8d$1@xxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> > QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>
> Did the above call to QSqlDatabase::addDatabase() succeed?
>
> > /// next line gives warning: QSqlDatabasePrivate::database: unable to
open
> > database:
> > /// but actualy set up database name!
> > QSqlDatabase::database().setDatabaseName("aaa.dat");
>
> --
> Dimitri
--
[ signature omitted ]
Message 4 in thread
Hi,
> Yes, it did.
> Even code like this works perfectly fine:
> ----------
> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
Just to make sure, what is the result of:
db.isValid()
db.lastError().databaseText()
db.lastError().driverText()
I'm not saying this is not a Qt bug, I'm just trying to understand what could
be happening.
--
[ signature omitted ]
Message 5 in thread
---------------------------
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QMessageBox::information(NULL, "with db",
".isValid()=" + QString(db.isValid()?"true":"false") + "\n" +
".lastError().databaseText()=" + db.lastError().databaseText() +
"\n" +
".lastError().driverText()=" + db.lastError().driverText());
---------------------------
shows ".isValid()=true" and empty strings for both calls to db.lastError().
No warnings.
---------------------------
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QMessageBox::information(NULL, "with QSqlDatabase::database()",
".isValid()=" +
QString(QSqlDatabase::database().isValid()?"true":"false") + "\n" +
".lastError().databaseText()=" +
QSqlDatabase::database().lastError().databaseText() + "\n" +
".driverText()=" +
QSqlDatabase::database().lastError().driverText());
---------------------------
Three warnings one after another and same results: true, empty, empty
---------------------------
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
QSqlDatabase::database().setDatabaseName("aaa.dat");
QMessageBox::information(NULL, "with QSqlDatabase::database()",
".isValid()=" +
QString(QSqlDatabase::database().isValid()?"true":"false") + "\n" +
".lastError().databaseText()=" +
QSqlDatabase::database().lastError().databaseText() + "\n" +
".driverText()=" +
QSqlDatabase::database().lastError().driverText());
---------------------------
One warning only and same results: true, empty, empty
"Dimitri" <dimitri@xxxxxxxxxxxxx> wrote in message
news:f0657e$7ck$1@xxxxxxxxxxxxxxxxxxxxx
> Hi,
>
> > Yes, it did.
> > Even code like this works perfectly fine:
> > ----------
> > QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>
> Just to make sure, what is the result of:
> db.isValid()
> db.lastError().databaseText()
> db.lastError().driverText()
>
> I'm not saying this is not a Qt bug, I'm just trying to understand what
could
> be happening.
>
> --
> Dimitri
--
[ signature omitted ]
Message 6 in thread
Do this if you have "aaa.dat" as database :
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("aaa.dat");
db.open();
QSqlQuery qry;
qry.exec("create table t(id integer)");
db.close();
Bye!
dj
> Yes, it did.
> Even code like this works perfectly fine:
> ----------
> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> QSqlDatabase::database().setDatabaseName("aaa.dat");
> QSqlDatabase::database().open();
> QSqlQuery qry;
> qry.exec("create table t(id integer);");
> QSqlDatabase::database().close();
> ----------
> But the very first attempt to call QSqlDatabase::database() - gives a
> warning. Subsequent calls go without any problem.
>
> "Dimitri" <dimitri@xxxxxxxxxxxxx> wrote in message
> news:f062dc$h8d$1@xxxxxxxxxxxxxxxxxxxxx
>
>> Hi,
>>
>>
>>> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
>>>
>> Did the above call to QSqlDatabase::addDatabase() succeed?
>>
>>
>>> /// next line gives warning: QSqlDatabasePrivate::database: unable to
>>>
> open
>
>>> database:
>>> /// but actualy set up database name!
>>> QSqlDatabase::database().setDatabaseName("aaa.dat");
>>>
>> --
>> Dimitri
>>
>
>
> --
> To unsubscribe - send a mail to qt-interest-request@xxxxxxxxxxxxx with "unsubscribe" in the subject or the body.
> List archive and information: http://lists.trolltech.com/qt-interest/
>
>
>
>
--
[ signature omitted ]
Message 7 in thread
George Brink wrote:
> Assistant say about QSqlDatabase::addDatabase:
> If connectionName is not specified, the newly added database connection
> becomes the default database connection for the application, and subsequent
> calls to database() without a database name parameter will return a
> reference to it.
>
> So, why next code give me warning?
[...]
> int main(int argc, char *argv[])
> {
> qInstallMsgHandler(myMessageOutput);
>
> QApplication app(argc, argv);
>
> QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
> /// next line gives warning: QSqlDatabasePrivate::database: unable to open
> database:
> /// but actualy set up database name!
> QSqlDatabase::database().setDatabaseName("aaa.dat");
>
> return 0;
> }
Because QSqlDatabase:database() tries to open the database, which fails
because the database name isn't set yet.
The documentation of QSqlDatabase::database() says:
"Returns the database connection called connectionName. The database
connection must have been previously added with addDatabase(). *If open
is true (the default) and the database connection is not already open it
is opened now.* If no connectionName is specified the default connection
is used. If connectionName does not exist in the list of databases, an
invalid connection is returned."
See: http://doc.trolltech.com/4.2/qsqldatabase.html#database
--
[ signature omitted ]
Message 8 in thread
> Because QSqlDatabase:database() tries to open the database, which fails
> because the database name isn't set yet.
Oh! I see it now, thanks.
But the next question is: why second parameter for QSqlDatabase::database()
is true by default? What's the use to open database automaticaly while I am
trying to get reference to it? I think it should not change database
open-status at all.
--
[ signature omitted ]
Message 9 in thread
George Brink wrote:
>> Because QSqlDatabase:database() tries to open the database, which fails
>> because the database name isn't set yet.
> Oh! I see it now, thanks.
> But the next question is: why second parameter for QSqlDatabase::database()
> is true by default? What's the use to open database automaticaly while I am
> trying to get reference to it? I think it should not change database
> open-status at all.
>
>
Because in most cases you really want a reference to an open connection
and not to a closed one. In your case you already have the reference,
QSqlDatabase::addDatabase() returned it to you, so the call to
QSqlDatabase::database() is uncalled-for.
Look at the code from your original post and substitute the line:
QSqlDatabase::database().setDatabaseName("aaa.dat");
with
db.setDatabaseName("aaa.dat");
and your problem goes away.
See the documentation for a detailed description of how to use the QtSql
module and its classes.
--
[ signature omitted ]
Message 10 in thread
"Anders Larsen" <alarsen@xxxxxxxxxxxxx> wrote in message
news:f07vc3$v8f$1@xxxxxxxxxxxxxxxxxxxxx
> George Brink wrote:
> > But the next question is: why second parameter for
QSqlDatabase::database()
> > is true by default? What's the use to open database automaticaly while I
am
> > trying to get reference to it? I think it should not change database
> > open-status at all.
> >
>
> Because in most cases you really want a reference to an open connection
> and not to a closed one.
WHY???? If I want opened connection, I will take reference to connection,
check it status and if needed, I will open it myself (with proper
error-checking if needed). But to open a connection with specialy defined
parametrs, I first need a reference to _some_ connection.
In any case, function "get reference to object" should not change the state
of this object in any way.
I believe, method QSqlDatabase::database() should not have second parameter
at all.
> In your case you already have the reference,
> QSqlDatabase::addDatabase() returned it to you, so the call to
> QSqlDatabase::database() is uncalled-for.
Not correct. Code in my original post is an example for strange behavior
I've found in a big project. In big project, I had registration of sql
driver in one module and connection setup in another. It was convinient from
incapsulation point of view. Now I am forced to create reference to database
as global object.
--
[ signature omitted ]
Message 11 in thread
George Brink wrote:
>> Because in most cases you really want a reference to an open connection
>> and not to a closed one.
> WHY???? If I want opened connection, I will take reference to connection,
> check it status and if needed, I will open it myself (with proper
> error-checking if needed). But to open a connection with specialy defined
> parametrs, I first need a reference to _some_ connection.
> In any case, function "get reference to object" should not change the state
> of this object in any way.
> I believe, method QSqlDatabase::database() should not have second parameter
> at all.
Because once you've set up the connection there isn't a lot you can do
with it except from run queries on it, and for that it must be open.
Use QSqlDatabase::addDatabase() to set up the connection and
QSqlDatabase::database() when you want to run queries on it.
If you really don't want QSqlDatabase::database() to open the connection
just give the connection a name when you create it with addDatabase()
and call database() with this name and false as parameters.
--
[ signature omitted ]