QTデータベース(QSQLITE)の使用(3つのremoveDatabase:接続addDatabase:重複ソリューション)

QSqlDatabasePrivate

プロンプト情報(エラーではありません。つまり、コンソールは毎回通知します)

QSqlDatabasePrivate :: removeDatabase:接続「cond」はまだ使用中です。すべてのクエリは機能しなくなります
。QSqlDatabasePrivate:: addDatabase:重複する接続名「cond」、古い接続は削除されました
。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;
    }
}

説明

上記の機能を使用します

[静的] bool QSqlDatabase ::に接続名を含めるかどうかが含まれます(含まれている場合は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.
[静的] 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.
[静的] 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