I am in the process of converting a .net / c# application over to Qt. I do not work with Qt / C++ everyday so bear with me.
The application uses SQLite and MS SQL Server simultaneously. Once I have established a connection, it is left open as the data transactions are quite high for both data stores and I don't want to waste time opening and closing the connection between transactions.
My aim was to create a generic manager class that could be used for both database type. The class works as it should when either connection is open without the other. However, when I attempt to establish a connection to both simultaneously, I receive the errors:
QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
So, I have read about connection naming However, I then run into problems accessing the database. I have posted the contents of my database manager class without connection naming. I am looking for suggestions on how I should be handling this in a clean and generic way. Thanks!
.h file
#ifndef DATABASEMGR_H
#define DATABASEMGR_H
#include <QObject>
#include <QSqlDatabase>
#include <QVariant>
#include <QSqlQuery>
#include <QSqlQueryModel>
#include <QSqlRecord>
class DatabaseMgr : public QObject
{
public:
DatabaseMgr(const QString &databaseType, const QString &connectionString, QObject *parent = 0);
~DatabaseMgr();
QVariant ExecuteScalar(const QString &cmd);
bool ExecuteNonQuery(const QString &cmd);
bool IsOpen() const;
bool Connect();
bool Disconnect();
QSqlQueryModel *GetQueryModel(const QString &cmd);
QSqlQueryModel *GetQueryModel(const QString &cmd, const QMap<QString, QVariant> ¶ms);
private:
QSqlDatabase mDb;
};
#endif // DATABASEMGR_H
.cpp
#include "databasemgr.h"
DatabaseMgr::DatabaseMgr(const QString &databaseType, const QString &connectionString, QObject *parent) {
mDb = QSqlDatabase::addDatabase(databaseType);
mDb.setDatabaseName(connectionString);
}
DatabaseMgr::~DatabaseMgr() {
if (mDb.open()) {
mDb.close();
}
}
QVariant DatabaseMgr::ExecuteScalar(const QString &cmd) {
QVariant mVariant;
if (mDb.isOpen()) {
QSqlQuery query;
if (query.exec(cmd)) {
while (query.next()) {
mVariant = query.value(0);
}
}
}
return mVariant;
}
bool DatabaseMgr::ExecuteNonQuery(const QString &cmd) {
if (mDb.isOpen()) {
QSqlQuery query;
if (query.exec(cmd)) {
return true;
} else {
//todo handle error
}
} else {
//todo handle error
}
return false;
}
bool DatabaseMgr::IsOpen() const {
return mDb.isOpen();
}
bool DatabaseMgr::Connect(){
if (!mDb.open()) {
//todo error opening database??
return false;
}
return true;
}
bool DatabaseMgr::Disconnect() {
return mDb.isOpen();
}
QSqlQueryModel *DatabaseMgr::GetQueryModel(const QString &cmd) {
QSqlQueryModel *model = new QSqlQueryModel;
if (mDb.isOpen()) {
model->setQuery(cmd, mDb);
}
return model;
}
QSqlQueryModel *DatabaseMgr::GetQueryModel(const QString &cmd, const QMap<QString, QVariant> ¶ms) {
QSqlQueryModel *model = new QSqlQueryModel;
if (mDb.isOpen()) {
QSqlQuery query;
query.prepare(cmd);
if (params.count() > 0) {
QMapIterator<QString, QVariant> i(params);
while (i.hasNext()) {
i.next();
query.bindValue(i.key(), i.value());
}
}
model->setQuery(query);
}
return model;
}
Example usages below:
main.cpp (the SQLite db is used for storing global application settings)
DatabaseMgr mSqliteDb("QSQLITE", app.applicationName() + ".db", &app);
mSqliteDb.Connect();
Program prgm(mSqliteDb, &app);
program.cpp
mMsSqlDb = new DatabaseMgr("QODBC3", GetMsSqlConnectionString());
if (mMsSqlDb->Connect()) {
AddToActivityLog("Connected to MS SQL DB");
} else {
AddToActivityLog("Error connecting to MS SQL DB");
}
Getting results from within program member function:
QString cmd = "SELECT DISTINCT some_things FROM the_table ORDER BY these_columns";
QSqlQueryModel *model = mMsSqlDb->GetQueryModel(cmd);
if (model->rowCount() > 0) {
for (int i = 0; i < model->rowCount(); ++i) {
//Do stuff...
}
}