Sqlite connection QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
-
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"));@
-
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. -
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?
-
thaks you for ur answer, we use it for our unit test.
I have another question abou threading may i ask u? -
@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.
-
@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 -
@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.