Help on my database class
-
i created a simple database wrapper so i just could call my query anywhere but i have a little problem on it. it displays "QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed."
myquery.h
@
#include <QSqlDatabase>
#include <QSqlQuery>class myQuery
{
public:
myQuery(QString, QString, QString, QString, int);
~myQuery();
QVector<QHash<QString, QVariant> > getData(QString);
int insertData(QString, QHash<QString, QVariant>);
bool updateData(QString, QHash<QString, QVariant>, QHash<QString, QString>);
bool deleteData(QString, QHash<QString, QString>);protected:
QSqlDatabase db;
QSqlQuery query;
};
@myquery.cpp
@
#include <QSqlRecord>
#include <QSqlError>
#include <QMessageBox>
#include <QVariant>
#include "myquery.h"myQuery::myQuery(QString host, QString username, QString password, QString db_name, int port)
{
if (!db.isOpen()) {
db = QSqlDatabase::addDatabase("QMYSQL");
db.setHostName(host);
db.setUserName(username);
db.setPassword(password);
db.setDatabaseName(db_name);
db.setPort(port);
if (!db.open()) QMessageBox::critical(0, "Connection Problem", db.lastError().text());
}
}myQuery::~myQuery()
{
QSqlDatabase::removeDatabase("QMYSQL");
}QVector<QHash<QString, QVariant> > myQuery::getData(QString sql)
{
QVector<QHash<QString, QVariant> > result;
if (!query.exec(sql)) {
QMessageBox::critical(0, "Query Problem : Getting Data", query.lastError().text());
} else {
QSqlRecord rec = query.record();
int i = 0;
while (query.next()) {
QHash<QString, QVariant> singleData;
for (int j=0; j < rec.count(); j++) {
singleData.insert(rec.fieldName(j), query.value(j));
}
result.insert(i++, singleData);
}
}
query.clear();
return result;
}int myQuery::insertData(QString table, QHash<QString, QVariant> data)
{
if (table.isEmpty()) {
QMessageBox::warning(0, "Query Problem : Inserting Data", "Database Table NOT Defined!");
return false;
}
if (data.size() == 0) {
QMessageBox::warning(0, "Query Problem : Inserting Data", "NO Data!");
return false;
}
QHashIterator<QString, QVariant> i(data), j(data); QString tCol, tVal;
while (i.hasNext()) {
i.next();
if (i.hasNext()) {
tCol.append(i.key() + ",");
tVal.append("?, ");
} else {
tCol.append(i.key());
tVal.append("?");
}
}
query.prepare("INSERT INTO " + table + "(" + tCol +") VALUE (" + tVal + ")");
while (j.hasNext()) {
j.next();
query.addBindValue(j.value());
}
int id = 0;
if (query.exec()) {
id = query.lastInsertId().toInt();
} else {
QMessageBox::critical(0, "Query Problem : Inserting Data", query.lastError().text());
}
query.clear();
return id;
}bool myQuery::updateData(QString table, QHash<QString, QVariant> data, QHash<QString, QString> where)
{
if (table.isEmpty()) {
QMessageBox::warning(0, "Query Problem : Updating Data", "Database Table NOT Defined!");
return false;
}
if (data.size() == 0) {
QMessageBox::warning(0, "Query Problem : Updating Data", "NO Data!");
return false;
}
if (where.size() == 0) {
QMessageBox::warning(0, "Query Problem : Updating Data", "NO Specific ID!");
return false;
}
QHashIterator<QString, QVariant> i(data), k(data); QHashIterator<QString, QString> j(where); QString tSet, tWhere;
while (i.hasNext()) {
i.next();
QVariant val = i.value();
if (i.hasNext()) {
if (val.isNull()) {
tSet.append(i.key() + "=NULL, ");
} else {
tSet.append(i.key() + "=?, ");
}
} else {
if (val.isNull()) {
tSet.append(i.key() + "=NULL");
} else {
tSet.append(i.key() + "=?");
}
}
}
while (j.hasNext()) {
j.next();
if (j.hasNext()) {
tWhere.append(j.key() + "='" + j.value() + "' AND ");
} else {
tWhere.append(j.key() + "='" + j.value() + "'");
}
}
query.prepare("UPDATE " + table + " SET " + tSet + " WHERE " + tWhere);
while (k.hasNext()) {
k.next();
query.addBindValue(k.value());
}
bool result = true;
if (!query.exec()) {
QMessageBox::critical(0, "Query Problem : Updating Data", query.lastError().text());
result = false;
}
query.clear();
return result;
}bool myQuery::deleteData(QString table, QHash<QString, QString> data)
{
if (table.isEmpty()) {
QMessageBox::warning(0, "Query Problem : Deleting Data", "Database Table NOT Defined!");
return false;
}
if (data.size() == 0) {
QMessageBox::warning(0, "Query Problem : Deleting Data", "NO Data!");
return false;
}
bool result = true;
QHashIterator<QString, QString> i(data);
while (i.hasNext()) {
i.next();
if (!query.exec("DELETE FROM " + table + " WHERE " + i.key() + "='" + i.value() + "'")) {
QMessageBox::critical(0, "Query Problem : Deleting Data", query.lastError().text());
result = false;
}
}
query.clear();
return result;
}
@am i doing it right in removing the connection or did i miss something.
already read the documentation but on this class, i dont know how to put it.thanks.
-
Well, as you manage the database connection in your class' constructor / destructor it would have been useful to see how your myQuery class is used, not how it looks like.
Most probably you are just creating another instance of your myQuery class (explicitly or implicitly by assignment or passing as argument for example) which obivously fail as your connection is created twice.
-
thanks for the reply. i used it like this...
@
void Window::sampleName() {
myQuery *q = new myQuery('host', 'username', 'password', 'db_name', 3306);
QVector<QHash<QString, QVariant> > items = q->getData("SELECT * FROM table");
q->~myQuery();
... process the items ...
}
@ -
From the code you posted it does not seem the query is executed twice, but maybe the sampleName method is called more than once? I suggest placing the database connection configuration out of the constructor. Moreover do not call the destructor directly, delete the object if you don't need it anymore.
-
You might call an objects destructor explicitly when placement new was used - otherwise just don't do it. Never.
In addition, once a QSqlDatabase connection is created it is accessible from everywhere. Every QtSql operation will use the default database connection (if no other connection is specified). The usual workflow is
@
int main(int argc, char* argv[])
{
QApplication application(argc, argv);
QSqlDatabase database(...);
if(database.open())
{
MainWindow mainWindow;mainWindow.show(); ...
@
This prevents the connection from beeing opened twice.It is quite easy to spot your error. Just place a breakpoint in the constructor and destructor of myClass and take a look at the order they are called. The stack trace will reveal who is calling the constructor or destructor out of order.
-
fluca1978 : the sampleName method doesnt called more than once and if ever it is called, the destructor should do the trick but it wasnt. and if i delete the object itself, it wont compile.
Lukas Geyer : thanks for the suggestion but i prefer connecting it when needed. that is why i need to close the connection. and i did put breakpoints and the removeDatabase() is not truly working so when i instantiate the object again, the annoying problem appear.
-
Well, I have taken another look at your code and you should take another look at QSqlDatabase::addDatabase() and QSqlDatabase::removeDatabase(). The former one takes the database type as parameter whereas the latter one takes a connection name as parameter.
In addition, there is no need to use addDatabase() / removeDatabase() if you just want to connect / disconnect. Use open() / close() instead. Keep in mind that all your queries have to be gone out of scope before closing the database connection.