Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved Unsolved Qt for Python
6 Posts 2 Posters 2.4k 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.
  • Y Offline
    Y Offline
    YassineKira
    wrote on last edited by
    #1

    Hello !

    After hours of research, I know that this problem has already been asked but I can't find a solution with PyQt5 :

    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
    

    First of all in the first function I initialize the base and the second function to create a search engine in my base and display it in a Qtablewidget (for you to understand the code a little): D.

    I tried with the remove function but I didn't understand the function well, design_prix: it is a value of EditLine.

    Thank you in advance.

    class Database_prix:
     
        is_instantiated = False
         
        def __init__(self):
            if not Database_prix.is_instantiated:
             #  print ("Database has been instantiated:")
                self.db = QSqlDatabase.addDatabase("QSQLITE")
                self.db.setDatabaseName("C:/Users/yatamant/Documents/Mes fichiers reçus/Dev_Env/database_prix.db")
                self.db.removeDatabase("QSQLITE")
                self.db.open()
                Database_prix.is_instantiated =True
            else:
                print("Has already been created")
      def get_prix_selectionne(self):
         
        global design_prix
     
        query = QSqlQuery()
     
        query_string = "SELECT PU.ref_pu, PU.desig_pu, PU.prix FROM PU WHERE replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( lower(desig_pu), 'á','a'), 'ã','a'), 'â','a'), 'é','e'), 'ê','e'), 'í','i'),'ó','o') ,'õ','o') ,'ô','o'),'ú','u'), 'ç','c') LIKE :des "
     
        query.prepare(query_string)
         
        query.bindValue(":des","%" + design_prix + "%")
     
        query.exec(query_string)
     
        record = query.record()
        column_number = record.count()
     
        header_list = []
     
        for i in range(column_number):
            header_list.append(record.field(i).name())
     
        result_list = []
     
        while query.next():
            sublist = []
            for i in range(column_number):
                sublist.append(query.value(i))
            result_list.append(sublist)
        return [header_list, result_list]
    
    jsulmJ 1 Reply Last reply
    0
    • Y YassineKira

      Hello !

      After hours of research, I know that this problem has already been asked but I can't find a solution with PyQt5 :

      QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
      QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.
      

      First of all in the first function I initialize the base and the second function to create a search engine in my base and display it in a Qtablewidget (for you to understand the code a little): D.

      I tried with the remove function but I didn't understand the function well, design_prix: it is a value of EditLine.

      Thank you in advance.

      class Database_prix:
       
          is_instantiated = False
           
          def __init__(self):
              if not Database_prix.is_instantiated:
               #  print ("Database has been instantiated:")
                  self.db = QSqlDatabase.addDatabase("QSQLITE")
                  self.db.setDatabaseName("C:/Users/yatamant/Documents/Mes fichiers reçus/Dev_Env/database_prix.db")
                  self.db.removeDatabase("QSQLITE")
                  self.db.open()
                  Database_prix.is_instantiated =True
              else:
                  print("Has already been created")
        def get_prix_selectionne(self):
           
          global design_prix
       
          query = QSqlQuery()
       
          query_string = "SELECT PU.ref_pu, PU.desig_pu, PU.prix FROM PU WHERE replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace( lower(desig_pu), 'á','a'), 'ã','a'), 'â','a'), 'é','e'), 'ê','e'), 'í','i'),'ó','o') ,'õ','o') ,'ô','o'),'ú','u'), 'ç','c') LIKE :des "
       
          query.prepare(query_string)
           
          query.bindValue(":des","%" + design_prix + "%")
       
          query.exec(query_string)
       
          record = query.record()
          column_number = record.count()
       
          header_list = []
       
          for i in range(column_number):
              header_list.append(record.field(i).name())
       
          result_list = []
       
          while query.next():
              sublist = []
              for i in range(column_number):
                  sublist.append(query.value(i))
              result_list.append(sublist)
          return [header_list, result_list]
      
      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

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

      Database_prix

      Do you have more than one instance of this class? I'm asking because you initialise the default connection in its __init__.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      Y 1 Reply Last reply
      0
      • jsulmJ jsulm

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

        Database_prix

        Do you have more than one instance of this class? I'm asking because you initialise the default connection in its __init__.

        Y Offline
        Y Offline
        YassineKira
        wrote on last edited by YassineKira
        #3

        @jsulm Yes i have just once, but remove this class from another file thats why i think i did mistake

        jsulmJ 1 Reply Last reply
        0
        • Y YassineKira

          @jsulm Yes i have just once, but remove this class from another file thats why i think i did mistake

          jsulmJ Offline
          jsulmJ Offline
          jsulm
          Lifetime Qt Champion
          wrote on last edited by
          #4

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

          but remove this class from another file thats why i think i did mistake

          But do you create it again later? Or do you create the default connection also somewhere else?

          https://forum.qt.io/topic/113070/qt-code-of-conduct

          1 Reply Last reply
          0
          • Y Offline
            Y Offline
            YassineKira
            wrote on last edited by YassineKira
            #5

            @jsulm no i dont create it again, i have one database.
            I do not create a default connection but i report a class Database_prix to a class "Mainwindow". if this is the problem ????

            jsulmJ 1 Reply Last reply
            0
            • Y YassineKira

              @jsulm no i dont create it again, i have one database.
              I do not create a default connection but i report a class Database_prix to a class "Mainwindow". if this is the problem ????

              jsulmJ Offline
              jsulmJ Offline
              jsulm
              Lifetime Qt Champion
              wrote on last edited by jsulm
              #6

              @YassineKira
              "QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed." - this means you're creating the default connection again.

              "QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work."" - this means that there is an active query when you're removing database. Make sure you have no open queries when you remove the database.

              Also, as explained in the documentation you should not keep a variable holding your database (self.db) - you can always get it using https://doc.qt.io/qt-5/qsqldatabase.html#database (QSqlDatabase already manages all your connections).
              From QSqlDatabase documentation:
              "Warning: It is highly recommended that you do not keep a copy of the QSqlDatabase around as a member of a class, as this will prevent the instance from being correctly cleaned up on shutdown. If you need to access an existing QSqlDatabase, it should be accessed with database(). If you chose to have a QSqlDatabase member variable, this needs to be deleted before the QCoreApplication instance is deleted, otherwise it may lead to undefined behavior."

              https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3

              • Login

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