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?
static
portion of code. Not executing everytime Test
class is initialized. You can have a setup
function to handle all that work.If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With