/****************************************************************************
** 数据库连接管理
****************************************************************************/
#ifndef SQLDATABASE_H
#define SQLDATABASE_H
#include <QObject>
#include <QSqlDatabase>
#include <QSqlQuery>
class SqlDataBase : public QObject
{
Q_OBJECT
public:
explicit SqlDataBase(const QString &dbName, QObject *parent = 0);
~SqlDataBase();
QList<QStringList> readData(const QString &cmd); //读数据
bool writeData(const QString &cmd); //写数据
private:
QSqlDatabase db;
QSqlQuery sqlQuery;
};
#endif // SQLDATABASE_H
#include "sqldatabase.h"
#include <QSqlDatabase>
#include <QMessageBox>
#include <QSqlError>
#include <QSqlRecord>
#include <QVariant>
#include <QStringList>
#include <QDebug>
SqlDataBase::SqlDataBase(const QString &dbName, QObject *parent) :
QObject(parent)
{
if(db.contains(dbName)) //这里直接将文件名作为连接名
db = QSqlDatabase::database(dbName);
else
db = QSqlDatabase::addDatabase("QSQLITE",dbName);
db.setDatabaseName(dbName);
if(!db.open())
QMessageBox::information(NULL,"提示",QString("数据库打开失败(%1)").arg(dbName),QMessageBox::Yes);
qDebug()<<db.isOpen()<<db.databaseName()<<db.connectionName();
}
SqlDataBase::~SqlDataBase()
{
if(db.isOpen())
db.close();
if(db.contains(db.connectionName())) //此处出现警告
db.removeDatabase(db.connectionName());
}
/***************************************************************
* @brief 读取数据库数据
* @param
* @return
**************************************************************/
QList<QStringList> SqlDataBase::readData(const QString &cmd)
{
qDebug()<<cmd;
sqlQuery = QSqlQuery(db);
if(!sqlQuery.exec(cmd))
{
qDebug()<<db.lastError();
QMessageBox::warning(NULL, "提示",QString("数据读取失败!%1").arg(db.lastError().text()));
return QList<QStringList>();
}
QList<QStringList> recordList;
while(sqlQuery.next())
{
QStringList valueList;
for(int index = 0;index < sqlQuery.record().count();index++)
valueList << sqlQuery.value(index).toString();
recordList.append(valueList);
}
return recordList;
}
/***************************************************************
* @brief 写入数据库数据
* @param
* @return
**************************************************************/
bool SqlDataBase::writeData(const QString &cmd)
{
qDebug()<<cmd;
sqlQuery = QSqlQuery(db);
if(!sqlQuery.exec(cmd))
{
qDebug()<<db.lastError();
QMessageBox::warning(NULL, "提示",QString("数据写入失败!%1").arg(db.lastError().text()));
return false;
}
return true;
}
当执行下列代码时,出现警告
QSqlDatabasePrivate::removeDatabase: connection 'test' is still in use, all queries will cease to work.
{
SqlDataBase database("test");
}
原因:
在析构函数中,执行了移除当前数据库连接操作。原文:QSqlDatabase outputs a warning if you try to remove a connection referenced by other QSqlDatabase objects. Use contains() to see if a given connection name is in the list of connections.
在移除数据库连接时,db变量并没有释放,仍然被SqlDataBase这个类引用,所以移除会出现警告
解决方法:
SqlDataBase::~SqlDataBase()
{
if(db.isOpen())
db.close();
db = QSqlDatabase();//必须添加这一行,提前释放db,使引用计数清除,下面的移除连接会出现警告
if(db.contains(db.connectionName()))
db.removeDatabase(db.connectionName());
}