From 10:00 CET Friday 22nd November we will adjust how the server works to deal with some recently reported problems. Therefore there may be a load problem, if you experience more problems than usual trying to access the forum then please PM AndyS or any of the moderators so they can inform me.


Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.



  • Hello Gurus,

    I am trying to fetch data from an SQLite database, it works fine, however when I try to call the function again it works but with an debug message

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

    here is my code, do I close the connection correctly? thanks

    @QSqlQuery MainForm::QuerydB(QString Query, QString dataBaseFile)
    {
    //QSqlQuery result;
    //QString dbname;

    database = new QSqlDatabase();
    //set database driver to QSQLITE
    *database = QSqlDatabase::addDatabase("QSQLITE");
    database->setDatabaseName(dataBaseFile);
    qDebug() << dataBaseFile;
    
    if(!database->open())
    {
        QMessageBox::warning(0,"Error","Couldn't open setting database");
        //qApp->setProperty("DatabaseOpen","ERROR");
    }
    
    
     // read data
     QSqlQuery result (Query, *database);
    
    //close database;
     QString connname = database->connectionName();
    
    
     database->close();
     database->removeDatabase(dataBaseFile);
     delete database;
    
     return result;
    

    }
    @



  • I think you have to call QSqlDatabase::addDatabase("QSQLITE"); once in your program



  • But how can I then make it inform of a function?



  • You may want to look "here":http://qt-project.org/forums/viewthread/18722/.



  • you just have to call it in the ctor of you class i think



  • Normally you don't need to open the database connection more than once within your application. You also want to look at the sample code "here":http://doc.qt.nokia.com/4.7-snapshot/qsqldatabase.html#details.

    Since each database connection will only have a single instance within the whole application you are able to access it via QSqlDatabase::connection().

    I would suggest that you have three static functions in your MainForm. One that opens the database connection, one the exectues a query and the third to close the database. The first and the third are called i.e. in the MainForm constructor/destructor, as Neutron Stern wrote, the second whenever you need a query.

    Please read the mentioned documentation there you will find the needed help.



  • For all people who looks for a good solution, this is what i found, tried and works fine:
    (PS : I have an application, which has more then one project in it, means more then one sql-connection)

    Call addDatabase() only once in your application (see second parameter, can be anything)

    @QSqlDatabase::addDatabase("QSQLITE", "MyDBConnectionName");@

    Do this first whenever you want to do some stuff with your DB

    @if (QSqlDatabase::contains("MyDBConnectionName"))
    {
    ...e.g. query@

    Now you must get an instance to db-object

    @QSqlDatabase sqlDatabase = QSqlDatabase::database("MyDBConnectionName");@

    Add DB information to your query

    @QSqlQuery query(QSqlDatabase::database("MyDBConnectionName"));@


Log in to reply