Here my problem; in such a case it complains about duplicate connections with same connection name:
Test::Test(QString connectionName)
{
db=QSqlDatabase::addDatabase("QMYSQL",connectionName);
}
int main(int argc, char *argv[])
{
QString connectionName=QString("test");
QCoreApplication a(argc, argv);
Test myDb(connectionName);
Test myDb2(connectionName);
return a.exec();
}
Here my solution:
Test::Test(QString connectionName)
{
if(!QSqlDatabase::contains(connectionName))
db=QSqlDatabase::addDatabase("QMYSQL",connectionName);
else
db=QSqlDatabase::database(connectionName);
}
int main(int argc, char *argv[])
{
QString connectionName=QString("test");
QCoreApplication a(argc, argv);
{
Test myDb(connectionName);
Test myDb2(connectionName);
}
QSqlDatabase::removeDatabase(connectionName);
return a.exec();
}
1-)Is this a good way to handle this problem?
2-)Do you have another suggestion?
3-)Do you think that this is a drawback for Qt?