views:

54

answers:

2

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?

A: 
  1. --
  2. I would prefer adding the Database connection in a static portion of code. Not executing everytime Test class is initialized. You can have a setup function to handle all that work.
  3. No, it's not. It's by design. Usually you should not have to create/open a new DB connection every time you create a class instance.
Pablo Santa Cruz
For 2: What if I have separate classes which use same connection name in order to connect database?
metdos
For 3: I resemble it to global variable, is it some kind of bad usage, isn't it?
metdos
@metdos: For 2: you can pass around DB handle, instead of passing the connection name. Or, pass the connection name, but don't add it, just retrieve the connection by its name from QSqlDatabase.
Pablo Santa Cruz
@metdos: For 3: Not at all. If you thing about it, QSqlDatabase is like a global variable holding all DB connections for that QT session.
Pablo Santa Cruz
@Pablo Santa Cruz: Now, I edited, I guess you meant this.
metdos
Yes, it's better. I would still prefer to have that initialization in a static/global method and in your constructor you will only have: db=QSqlDatabase::database(connectionName);
Pablo Santa Cruz
I couldn't see how Can I manage different classes (Such as Test1 test1; Test2 test2;) with the static way you mentioned.
metdos
A: 

Just give your connections different names:

int main(int argc, char *argv[])
{
    QString connectionName("test");
    QString connectionName2("test2");
    QCoreApplication a(argc, argv);

    Test myDb(connectionName);
    Test myDb2(connectionName2);

    return a.exec();
}
PiedPiper
In the case of usage of independent libraries simultaneously, how can we prevent using same connection name?
metdos
If you don't have access to the source code I don't think you can prevent two libraries from using the same connection name. But you could file a bug-report with the developers of both libraries suggesting they use unique names.
PiedPiper