QT数据库的使用(QSQLITE) (三 removeDatabase: connection addDatabase: duplicate 解决方法)

QSqlDatabasePrivate

提示信息(非错误,就是每次都会控制台提醒)

QSqlDatabasePrivate::removeDatabase: connection ‘cond’ is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name ‘cond’, old connection removed.
QSqlDatabasePrivate::removeDatabase:连接’cond’仍在使用中,所有查询将停止工作。
QSqlDatabasePrivate::addDatabase:重复连接名称’cond’,旧连接删除。

以上这个提示,每次创建连接都会提醒,我以为使用
db.close();
db.removeDatabase(nmDatabase);
会有效果,但是还是一直提醒,很是无解。

最终解决办法:

.h 头文件

QSqlDatabase db ;       //声明全局变量
//    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "cond");  之前是声明并 创建连接

.cpp源文件 创建连接的函数

/**
 * @brief MyCondDB::createConnection
 * @param nmDataBase
 * @return
 * 创建连接
 */
bool MyCondDB::createConnection(QString nmDataBase)
{
    
    
    bool ok;
    //以后通过"cond"与数据库进行连接了
   if(QSqlDatabase::contains("cond"))
    {
    
    
        db = QSqlDatabase::database("cond");
    }else
    {
    
    
        db = QSqlDatabase::addDatabase("QSQLITE","cond");
    }
    qDebug() << QObject::tr("---------------------------------database connection name:%1").arg(db.connectionName());
    db.setDatabaseName(nmDataBase);
    ok = db.open();
    QSqlQuery query(db);
    QSqlError lastError = query.lastError();
    if(!ok)
    {
    
    
        qDebug() << QString("DataBase Open Lose:")<<lastError;
        return false;
    }else
    {
    
    
        qDebug() << QString("DataBase Open Success");
        return true;
    }
}

解释

以上函数用到了

[static] bool QSqlDatabase::contains 是否包含连接名 (包含返回true,不包含返回false)
[static] bool QSqlDatabase::contains
(const QString &connectionName = QLatin1String( defaultConnection ))
Returns true if the list of database connections contains connectionName; 
otherwise returns false.
Note: This function is thread-safe  这个函数的线程是安全的
See also connectionNames(), database(), and Threads and the SQL Module.
[static] QSqlDatabase QSqlDatabase::database
[static] QSqlDatabase QSqlDatabase::database(const QString &connectionName = QLatin1String( defaultConnection ), bool open = true)
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.
Note: This function is thread-safe
See also isOpen() and Threads and the SQL Module.
[static] QSqlDatabase QSqlDatabase::addDatabase
	[static] QSqlDatabase QSqlDatabase::addDatabase(const QString &type, const
QString &connectionName = QLatin1String( defaultConnection ))
	Adds a database to the list of database connections using the driver type and the 
connection name connectionName. If there already exists a database connection
called connectionName, that connection is removed.
	The database connection is referred to by connectionName. The newly added
database connection is returned.
	If type is not available or could not be loaded, isValid() returns false.
	If connectionName is not specified, the new connection becomes the default 
onnection for the application, and subsequent calls to database() without the
connection name argument will return the default connection. If a connectionName is 
provided here, use database(connectionName) to retrieve the connection.
	Warning: If you add a connection with the same name as an existing connection, 
the new connection replaces the old one. If you call this function more than once 
without specifying connectionName, the default connection will be the one replaced.
	Before using the connection, it must be initialized. e.g., call some or all of
setDatabaseName(), setUserName(), setPassword(), setHostName(), setPort(), 
and setConnectOptions(), and, finally, open().
	Note: This function is thread-safe
	See also database(), removeDatabase(), and Threads and the SQL Module.
以上就是每次创建连接先判断一次,是创建新的连接还是使用旧的连接名。
在调用完毕后 最后要db.close();
void QSqlDatabase::close()
Closes the database connection, freeing any resources acquired, and invalidating any existing QSqlQuery objects that are used with the database.
This will also affect copies of this QSqlDatabase object.
See also removeDatabase().
db.removeDatabase(nmDatabase);
[static] void QSqlDatabase::removeDatabase(const QString &connectionName)
Removes the database connection connectionName from the list of database connections.
Warning: There should be no open queries on the database connection when this function is called, otherwise a resource leak will occur.
Example:

  // WRONG
  QSqlDatabase db = QSqlDatabase::database("sales");
  QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
  QSqlDatabase::removeDatabase("sales"); // will output a warning

  // "db" is now a dangling invalid database connection,
  // "query" contains an invalid result set

The correct way to do it:

  {
      QSqlDatabase db = QSqlDatabase::database("sales");
      QSqlQuery query("SELECT NAME, DOB FROM EMPLOYEES", db);
  }
  // Both "db" and "query" are destroyed because they are out of scope
  QSqlDatabase::removeDatabase("sales"); // correct

To remove the default connection, which may have been created with a call to addDatabase() not specifying a connection name, you can retrieve the default connection name by calling connectionName() on the database returned by database(). Note that if a default database hasn't been created an invalid database will be returned.
Note: This function is thread-safe
See also database(), connectionName(), and Threads and the SQL Module.

这样就会规避掉每次都要重复删除旧链接,创建新连接了。

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_45646951/article/details/108712344
今日推荐