什么是正确的方式QSqlDatabase& QSqlQuery? [英] What is the correct way of QSqlDatabase & QSqlQuery?
问题描述
我对手册感到困惑,应该这样工作:
I got confused with the manual , should i work like this:
{
QSqlDatabase db = QSqlDatabase::addDatabase (...);
QSqlQuery query (db);
query.exec (...);
}
QSqlDatabase::removeDatabase (...);
正如文档指出的, query
db
将被自动解构。
但是那么高效吗?
As the document points out, query
or db
will be deconstructed automatically.
But is that efficient ?
好吧,如果我在类中缓存 db
:
Well , if i cache db
inside a class , like the following:
class Dummy {
Dummy() {
db = QSqlDatabase::addDatabase (...);
}
~Dummy() {
db.close();
}
bool run() {
QSqlQuery query (db);
bool retval = query.exec (...);
blabla ...
}
private:
QSqlDatabase db;
};
有时我会看到类似的警告:
Sometimes i could see warnings like:
QSqlDatabasePrivate::removeDatabase: connection 'BLABLA' is still in use, all queries will cease to work.
$ b c>。
Even if i didn't call run()
.
推荐答案
当您创建 QSqlDatabase
c $ c> addDatabase 或者当你调用 removeDatabase
时,你只是关联或取消关联一个元组(driver,hostname:port,database名称,用户名/密码)添加到名称(如果未指定连接名称,则为默认连接名称)。
SQL驱动程序实例化,但只有在调用 QSqlDatabase :: open
时才会打开数据库。
When you create a QSqlDatabase
object with addDatabase
or when you call removeDatabase
, you are merely associating or disassociating a tuple (driver, hostname:port, database name, username/password) to a name (or to the default connection name if you don't specify a connection name).
The SQL driver is instantiated, but the database will only be opened when you call QSqlDatabase::open
.
该连接名称在应用程序范围内定义。因此,如果你在每个使用它的对象中调用 addDatabase
,你将改变所有使用相同对象的 QSqlDatabase
连接名称,并使在其上活动的所有查询失效。
That connection name is defined application-wide. So if you call addDatabase
in each of the objects that use it, you are changing all QSqlDatabase
objects that uses the same connection name and invalidating all queries that were active on them.
您引用的第一个代码示例显示如何通过确保以下内容正确地断开连接名称:
The first code example you cited shows how to correctly disassociate the connection name, by ensuring that:
- $通过调用
- $
>
d
:当$
(QSqlDatabase
对象超出时,removeDatabaseclose()
范围)。
关闭数据库之前,从 QSqlDatabase
中分离出了 all QSqlQuery
> QSqlQuery :: finish()
,当 QSqlQuery
对象超出作用域时自动启动, - all
QSqlQuery
are detached from theQSqlDatabase
before closing the database by callingQSqlQuery::finish()
, which is automatic when theQSqlQuery
object goes out of scope, - all
QSqlDatabase
with the same connection name areclose()
d when you callQSqlDatabase::removeDatabase
(close()
is also called automatically when theQSqlDatabase
object goes out of scope).
创建QSql数据库时,取决于您希望连接在应用程序生命周期(2),您可以:
When you create the QSqlDatabase, depending on whether you want the connection to stay open for the application lifetime (1) or just when needed (2), you can:
-
保留一个
QSqlDatabase
实例在一个类中(例如,在您的mainwindow中),并通过直接传递QSqlDatabase
或只传递连接名称使用它在需要它的其他对象您传递到QSqlDatabase :: database
以获取QSqlDatabase
实例。QSqlDatabase :: database
使用QHash
来检索QSqlDatabase
它的名称,所以它可能比在对象和函数之间直接传递QSqlDatabase
对象的速度可以忽略不计,如果你使用默认连接,你甚至不必传递任何地方,只需调用QSqlDatabase :: database()
,不带任何参数。
keep a single
QSqlDatabase
instance in one single class (for example, in your mainwindow), and use it in other objects that needs it either by passing theQSqlDatabase
directly or just the connection name that you pass toQSqlDatabase::database
to get theQSqlDatabase
instance back.QSqlDatabase::database
usesQHash
to retrieve aQSqlDatabase
from its name, so it is probably negligibly slower than passing theQSqlDatabase
object directly between objects and functions, and if you you use the default connection, you don't even have to pass anything anywhere, just callQSqlDatabase::database()
without any parameter.
// In an object that has the same lifetime as your application
// (or as a global variable, since it has almost the same goal here)
QSqlDatabase db;
// In the constructor or initialization function of that object
db = QSqlDatabase::addDatabase("QSQLDRIVER", "connection-name");
db.setHostname(...);
// ...
if(!this->db.open()) // open it and keep it opened
{
// Error handling...
}
// --------
// Anywhere you need it, you can use the "global" db object
// or get the database connection from the connection name
QSqlDatabase db = QSqlDatabase::database("connection-name");
QSqlQuery query(db);
配置 QSqlDatabase
打开它来测试参数是否正确,并且丢弃实例。连接名称仍然可以在任何地方访问,但数据库必须重新打开:
configure the QSqlDatabase
once, open it to test that the parameters are correct, and ditch the instance. The connection name, will still be accessible anywhere, but the database will have to be reopened:
{
// Allocated on the stack
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLDRIVER", "connection-name");
db.setHostname(...);
// ...
if(!this->db.open()) // test the connection
{
// Error handling
}
// db is closed when it goes out of scope
}
{
// Same thing as for (1), but by default database() opens
// the connection if it isn't already opened
QSqlDatabase db = QSqlDatabase::database("connection-name");
QSqlQuery query(db);
// if there is no other connection open with that connection name,
// the connection is closed when db goes out of scope
}
在这种情况下,请注意,你不应该明确地关闭数据库,因为你可以有多个对象使用相同的数据库连接(例如,如果函数A使用连接,并且调用B也使用连接。如果B在将控制权返回到A之前关闭连接,则连接也将关闭A,这可能是坏事)。
In that case, note that you shouldn't close the database explicitly, because you can have multiple objects using the same database connection in a reentrant manner (for example, if a function A use the connection and calls B which also use the connection. If B closes the connection before returning control to A, the connection will also be closed for A, which is probably a bad thing).
这篇关于什么是正确的方式QSqlDatabase& QSqlQuery?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!