5

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?

metdos
  • 13,411
  • 17
  • 77
  • 120

2 Answers2

1
  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
  • 176,835
  • 32
  • 241
  • 292
  • For 2: What if I have separate classes which use same connection name in order to connect database? – metdos Sep 27 '10 at 11:52
  • For 3: I resemble it to global variable, is it some kind of bad usage, isn't it? – metdos Sep 27 '10 at 11:54
  • @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 Sep 27 '10 at 12:29
  • @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 Sep 27 '10 at 12:29
  • 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 Sep 27 '10 at 13:06
  • I couldn't see how Can I manage different classes (Such as Test1 test1; Test2 test2;) with the static way you mentioned. – metdos Sep 27 '10 at 13:12
0

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
  • 5,735
  • 1
  • 30
  • 40
  • In the case of usage of independent libraries simultaneously, how can we prevent using same connection name? – metdos Sep 28 '10 at 05:18
  • 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 Sep 28 '10 at 23:38