Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

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

Scheduled Pinned Locked Moved C++ Gurus
13 Posts 6 Posters 28.6k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SherifOmran
    wrote on 30 Jul 2012, 08:32 last edited by
    #1

    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;
    

    }
    @

    C 1 Reply Last reply 7 May 2023, 07:40
    0
    • N Offline
      N Offline
      Neutron Stein
      wrote on 1 Aug 2012, 12:53 last edited by
      #2

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

      Never Seen !

      1 Reply Last reply
      1
      • S Offline
        S Offline
        SherifOmran
        wrote on 1 Aug 2012, 15:26 last edited by
        #3

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

        1 Reply Last reply
        0
        • F Offline
          F Offline
          franku
          wrote on 1 Aug 2012, 15:31 last edited by
          #4

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

          This, Jen, is the internet.

          1 Reply Last reply
          0
          • N Offline
            N Offline
            Neutron Stein
            wrote on 1 Aug 2012, 15:39 last edited by
            #5

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

            Never Seen !

            1 Reply Last reply
            0
            • F Offline
              F Offline
              franku
              wrote on 1 Aug 2012, 15:39 last edited by
              #6

              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.

              This, Jen, is the internet.

              1 Reply Last reply
              0
              • Z Offline
                Z Offline
                zakarrrr
                wrote on 29 Jun 2014, 14:34 last edited by
                #7

                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"));@

                If a person is doing things behind you, he s clearly an ...
                Because he is an ..., he will tell any stories, which will make you think he is ok! -.-

                N 1 Reply Last reply 7 May 2023, 06:17
                1
                • Z zakarrrr
                  29 Jun 2014, 14:34

                  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"));@

                  N Offline
                  N Offline
                  nasimChildOfDesert
                  wrote on 7 May 2023, 06:17 last edited by
                  #8

                  @zakarrrr

                  Hi
                  I have the sampe problem and I solve it by this command :
                  if (QSqlDatabase::contains("NameOfConnection"))
                  {
                  QSqlDatabase::removeDatabase("NameOfConnection");
                  return
                  }
                  QSqlDatabase::addDatabase("QSQLITE", "NameOfConnection");
                  I hope it will beuseful.

                  1 Reply Last reply
                  0
                  • S SherifOmran
                    30 Jul 2012, 08:32

                    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;
                    

                    }
                    @

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 7 May 2023, 07:40 last edited by
                    #9

                    Your call to removeDatabase() is wrong - it takes the database connection name, not a filename: https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase

                    Also why do you call add/removeDatabase every time instead openening it once as described in the documentation?

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    N 1 Reply Last reply 7 May 2023, 09:00
                    1
                    • C Christian Ehrlicher
                      7 May 2023, 07:40

                      Your call to removeDatabase() is wrong - it takes the database connection name, not a filename: https://doc.qt.io/qt-6/qsqldatabase.html#removeDatabase

                      Also why do you call add/removeDatabase every time instead openening it once as described in the documentation?

                      N Offline
                      N Offline
                      nasimChildOfDesert
                      wrote on 7 May 2023, 09:00 last edited by
                      #10

                      @Christian-Ehrlicher

                      thaks you for ur answer, we use it for our unit test.
                      I have another question abou threading may i ask u?

                      C 1 Reply Last reply 7 May 2023, 09:07
                      0
                      • N nasimChildOfDesert
                        7 May 2023, 09:00

                        @Christian-Ehrlicher

                        thaks you for ur answer, we use it for our unit test.
                        I have another question abou threading may i ask u?

                        C Offline
                        C Offline
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on 7 May 2023, 09:07 last edited by
                        #11

                        @nasimChildOfDesert said in Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.:

                        I have another question abou threading may i ask u?

                        You have to create a new db connection per thread. There are a lot of threads about this in the forum.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        N 1 Reply Last reply 7 May 2023, 09:19
                        0
                        • C Christian Ehrlicher
                          7 May 2023, 09:07

                          @nasimChildOfDesert said in Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.:

                          I have another question abou threading may i ask u?

                          You have to create a new db connection per thread. There are a lot of threads about this in the forum.

                          N Offline
                          N Offline
                          nasimChildOfDesert
                          wrote on 7 May 2023, 09:19 last edited by
                          #12

                          @Christian-Ehrlicher
                          no imean do you know agood document about threading, mutex, socketprograming TCP
                          there are a lot of info on the net but most of them are not complete and no practical sample

                          C 1 Reply Last reply 7 May 2023, 09:20
                          0
                          • N nasimChildOfDesert
                            7 May 2023, 09:19

                            @Christian-Ehrlicher
                            no imean do you know agood document about threading, mutex, socketprograming TCP
                            there are a lot of info on the net but most of them are not complete and no practical sample

                            C Offline
                            C Offline
                            Christian Ehrlicher
                            Lifetime Qt Champion
                            wrote on 7 May 2023, 09:20 last edited by
                            #13

                            @nasimChildOfDesert said in Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.:

                            threading, mutex, socketprograming TCP

                            You don't need threading for sockets/tcp when you use Qt. Seaching for 'Qt threading' or looking at the QTcpSocket documentation should be a good start.

                            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                            Visit the Qt Academy at https://academy.qt.io/catalog

                            1 Reply Last reply
                            1

                            • Login

                            • Login or register to search.
                            • First post
                              Last post
                            0
                            • Categories
                            • Recent
                            • Tags
                            • Popular
                            • Users
                            • Groups
                            • Search
                            • Get Qt Extensions
                            • Unsolved