Results 1 to 4 of 4

Thread: duplicate connection name 'qt_sql_default_connection'

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Question duplicate connection name 'qt_sql_default_connection'

    hi to all, I've centos 7.5 in VM, and having Qt5.7 in same VM.

    I am trying to build some program, in this a class connDB having static method as follows :-
    Qt Code:
    1. QSqlDatabase * connDB::connectDB(QSqlDatabase *db, QString *uname, QString *passwd, QString *dbname, QString *host )
    2. {
    3. db = new QSqlDatabase(QSqlDatabase::addDatabase("QMYSQL"));
    4. db->setDatabaseName(*dbname);
    5. db->setHostName(*host);
    6. db->setUserName(*uname);
    7. db->setPassword(*passwd);
    8. db->setPort(3306);
    9.  
    10. return db;
    11. }
    To copy to clipboard, switch view to plain text mode 

    now in a slot :-
    Qt Code:
    1. void MyMainWindow::on_pushButtonLogin_clicked()
    2. {
    3. QString user = ui->lineEditUserName->text().trimmed();
    4. QString passwd = ui->lineEditPassword->text().trimmed();
    5. QString dbname = "cbs";
    6. QString host = "serverora11gr2.db.net";
    7.  
    8. db = connDB::connectDB(db, &user, &passwd, &dbname, &host);
    9.  
    10. if(db->open())
    11. {
    12. QMessageBox::information(this, "Login", " Connection Succeeded");
    13. }
    14. else
    15. {
    16. QMessageBox::warning(this, "Login", "Coneection failure : " + db->lastError().text());
    17. }
    18.  
    19. db->close();
    20. QSqlDatabase::removeDatabase(dbname);
    21. delete db;
    22. }
    To copy to clipboard, switch view to plain text mode 
    when I tried to to run it is successfully runs but when i press login button more it shows following
    QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.

    how to overcome this problem.cbs login problem.jpg

  2. #2
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: duplicate connection name 'qt_sql_default_connection'

    how to overcome this problem.
    Read what the error message is telling you. It's a duplicate connection. Then look at your slot code and think about what it is doing. At a minimum, you should check to see if the DB is already open before trying to connect, not to mention checking to see if the pointer that is returned is non-null before trying to use it.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

  3. #3
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: duplicate connection name 'qt_sql_default_connection'

    i foud solution :-

    Qt Code:
    1. void MyMainWindow::on_pushButtonLogin_clicked()
    2. {
    3. QString user = ui->lineEditUserName->text().trimmed();
    4. QString passwd = ui->lineEditPassword->text().trimmed();
    5. QString dbname = "cbs";
    6. QString host = "serverora11gr2.db.net";
    7.  
    8.  
    9.  
    10. db = connDB::connectDB(db, &user, &passwd, &dbname, &host);
    11.  
    12. if(db->open())
    13. {
    14. QMessageBox::information(this, "Login", " Connection Succeeded");
    15. }
    16. else
    17. {
    18. QMessageBox::warning(this, "Login", "Coneection failure : " + db->lastError().text());
    19. }
    20.  
    21. db->close(); // added in this line this
    22. delete db; // added in this line this
    23. QSqlDatabase::removeDatabase(dbname); // added in this line this
    24. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by rahulvishwakarma; 18th March 2020 at 17:57.

  4. #4
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,230
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: duplicate connection name 'qt_sql_default_connection'

    Don't just add random code, see that it doesn't work, and then expect people here to solve the problem for you. Read the error messages. If the DB is "still in use" it probably means you haven't closed the DB before trying to remove the connection.

    And if you are connecting to the same database each time, why would you want to close or remove it? Keep the connection until you are really done with it, don't try to connect if it is already connected, and don't remove the connection if you will use it again. All you are doing is making your program less responsive because of the repeated connections and disconnections.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. Replies: 3
    Last Post: 19th November 2015, 20:50
  2. error while execution duplicate connection name
    By khadija123 in forum Qt Programming
    Replies: 1
    Last Post: 13th April 2012, 15:59
  3. 2 questions about qt_sql_default_connection error
    By mtnbiker66 in forum Qt Programming
    Replies: 0
    Last Post: 20th February 2012, 01:04
  4. Replies: 0
    Last Post: 14th July 2010, 14:40
  5. Replies: 3
    Last Post: 22nd June 2006, 17:27

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.