QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
-
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]
-
@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__.
-
@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?
-
@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 ???? -
@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."