Results 1 to 4 of 4

Thread: How to correctly close a database connection

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Nov 2010
    Location
    Cienfuegos, Cuba
    Posts
    16
    Thanks
    6
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly close a database connection

    Hi and thank you. Your solution works well but I don't want to create a QSqlDatabase every time I need to make a query so I solved the problem using another solution.

    Instead of removing the QSqlDatabase instance from my class I modified the destructor of the class. Now it looks like

    Qt Code:
    1. DAO::~DAO()
    2. {
    3. this->db = QSqlDatabase();
    4. QSqlDatabase::removeDatabase(this->connName);
    5. }
    To copy to clipboard, switch view to plain text mode 

    On the firts instruction of the destructor, the reference to my database is destroyed so now I can remove the database with the removeDatabase function. Tested and working perfect: all the warnings gone!

  2. #2
    Join Date
    Jan 2006
    Location
    Napoli, Italy
    Posts
    621
    Thanks
    5
    Thanked 86 Times in 81 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: How to correctly close a database connection

    QSqlDatabase uses Implicit data sharing in order to minimize copying.

    Using my version (suggested by Qt) you copy very small amount of data
    A camel can go 14 days without drink,
    I can't!!!

Similar Threads

  1. QSqlDatabase Connection Close on Destruction
    By Sanuden in forum Qt Programming
    Replies: 1
    Last Post: 1st September 2011, 15:32
  2. Connection With database
    By sudheer168 in forum Qt Programming
    Replies: 4
    Last Post: 22nd December 2010, 09:18
  3. how to reconnect CORRECTLY qmysql database?
    By yaseminyilmaz in forum Newbie
    Replies: 7
    Last Post: 12th January 2010, 13:09
  4. how to reconnect CORRECTLY qmysql database?
    By yaseminyilmaz in forum Qt Programming
    Replies: 0
    Last Post: 31st December 2009, 12:20
  5. Closing correctly sqlite connection?
    By 0xl33t in forum Newbie
    Replies: 1
    Last Post: 9th August 2009, 13:22

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
  •  
Qt is a trademark of The Qt Company.