Results 1 to 2 of 2

Thread: QSqlDatabase::removeDatabase problem

  1. #1
    Join Date
    Nov 2010
    Location
    planet earth
    Posts
    19
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default QSqlDatabase::removeDatabase problem

    this is my code :

    mydb.cpp
    Qt Code:
    1. #include <QSqlDatabase>
    2. QSqlDatabase MyDB::connectDatabase()
    3. {
    4. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    5. // below are usernames & passwords & ports & etc...
    6. return db;
    7. }
    8.  
    9. void MyDB::removeConnection()
    10. {
    11. QSqlDatabase::removeDatabase("QMYSQL");
    12. }
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. #include "mydb.h"
    2. MyDB mydb;
    3. MainWindow::MainWindow()
    4. {
    5. {
    6. QSqlDatabase db = mydb.connectDatabase();
    7. QSqlQuery query;
    8. // perform queries here
    9. query.clear();
    10. db.close();
    11. }
    12. mydb.removeDatabase(); // not working here
    13. }
    To copy to clipboard, switch view to plain text mode 

    i still get this error : QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed.
    what am i missing? i already read the docs about QSqlDatabase::removeDatabase()...
    thanks...

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: QSqlDatabase::removeDatabase problem

    the problem is not removeDatabase. The problem seems to be that you call MyDB::connectDatabase several times or create a new default connection. Use a connection name when you add a database to see what the problem is exactly. Further you don't need to return the QSqlDatabase object. Get it every time you need it via
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("your connection name");
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 0
    Last Post: 14th July 2010, 14:40
  2. Replies: 12
    Last Post: 2nd April 2010, 10:50
  3. Sqlite & QSqlDatabase problem
    By rsimone in forum Newbie
    Replies: 5
    Last Post: 29th July 2009, 22:52
  4. QSqlDatabase driver problem
    By croscato in forum Qt Programming
    Replies: 2
    Last Post: 21st November 2008, 17:16
  5. Replies: 10
    Last Post: 6th March 2006, 17:08

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.