PDA

View Full Version : QSqlDatabase::removeDatabase problem



xeroblast
8th December 2010, 05:15
this is my code :

mydb.cpp


#include <QSqlDatabase>
QSqlDatabase MyDB::connectDatabase()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
// below are usernames & passwords & ports & etc...
return db;
}

void MyDB::removeConnection()
{
QSqlDatabase::removeDatabase("QMYSQL");
}


mainwindow.cpp


#include "mydb.h"
MyDB mydb;
MainWindow::MainWindow()
{
{
QSqlDatabase db = mydb.connectDatabase();
QSqlQuery query;
// perform queries here
query.clear();
db.close();
}
mydb.removeDatabase(); // not working here
}


i still get this error : QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
what am i missing? i already read the docs about QSqlDatabase::removeDatabase()...
thanks...

Lykurg
8th December 2010, 07:50
the problem is not removeDatabase. The problem seems to be that you call MyDB::connectDatabase several times or create a new default connection. Use a connection name when you add a database to see what the problem is exactly. Further you don't need to return the QSqlDatabase object. Get it every time you need it via
QSqlDatabase db = QSqlDatabase::database("your connection name");