Results 1 to 7 of 7

Thread: Disconnecting from database after using QSqlTableModel

  1. #1
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Disconnecting from database after using QSqlTableModel

    Hi,

    I'm trying to create a GUI which has three buttons:
    • connectButton: Connects to a SQLite3 database file on my computer.
    • showTableButton: Shows a table from the database file in a QTableView.
    • disconnectButton: Disconnects from the database file so I can edit it externally without closing the QT GUI.


    The code for each button is:

    on_connectButton_clicked():
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE", "metadataDB");
    2. db.setDatabaseName("metadata.db");
    To copy to clipboard, switch view to plain text mode 

    on_showTableButton_clicked():
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("metadataDB");
    2. QSqlTableModel *model = new QSqlTableModel(this,db);
    3. model->setEditStrategy(QSqlTableModel::OnManualSubmit);
    4. model->setTable("TableName");
    5. model->select();
    6. QTableView *view = new QTableView;
    7. view->setModel(model);
    8. view->show();
    To copy to clipboard, switch view to plain text mode 

    on_disconnectButton_clicked()
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("metadataDB");
    2. db.close();
    3. QSqlDatabase::removeDatabase("metadataDB");
    To copy to clipboard, switch view to plain text mode 

    The program connects to the database and displays the table correctly. However disconnectButton() doesn't release the metadata.db file. So if I try and delete or rename the file I get "the process cannot access the file because it is being used by another process". If I close the QT GUI then metadata.db is accessible again. Similarly if I never call showTableButton then metadata.db is always accessible.

    If anyone could point out what I'm doing wrong I would greatly appreciate it. I suspect I need to delete the QTableModel but I'm not sure how to get a handle for it from inside disconnectButton().

    Regards,

    Robbie
    Attached Files Attached Files
    Last edited by RobbieClarken; 7th April 2009 at 13:48. Reason: added source files as attachment

  2. #2
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    did you try to do something like this?
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("metadataDB");
    2. db.close();
    3. QSqlDatabase::removeDatabase("metadataDB");
    4. db = QSqlDatabase();
    To copy to clipboard, switch view to plain text mode 
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  3. #3
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    Quote Originally Posted by spirit View Post
    did you try to do something like this?
    Qt Code:
    1. QSqlDatabase db = QSqlDatabase::database("metadataDB");
    2. db.close();
    3. QSqlDatabase::removeDatabase("metadataDB");
    4. db = QSqlDatabase();
    To copy to clipboard, switch view to plain text mode 
    Hi spirit,

    Thanks for the suggestion. Alas that doesn't work. Neither does redefining db.setDatabaseName(). I do suspect it will be something simple like that!

  4. #4
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    I don't look to your file only to this code on page, have you use somewhere
    Qt Code:
    1. db.open()
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Apr 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    Quote Originally Posted by mazurekwrc View Post
    I don't look to your file only to this code on page, have you use somewhere
    Qt Code:
    1. db.open()
    To copy to clipboard, switch view to plain text mode 
    You're right that I did forget that line. Unfortunately inserting it doesn't resolve my problem.

  6. #6
    Join Date
    Aug 2008
    Location
    Ukraine, Krivoy Rog
    Posts
    1,963
    Thanked 370 Times in 336 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    Quote Originally Posted by RobbieClarken View Post
    You're right that I did forget that line. Unfortunately inserting it doesn't resolve my problem.
    that's not an error, because you use QSqlDatabase::database and this method automatically opens database, from docs.
    QSqlDatabase QSqlDatabase::database ( const QString & connectionName = QLatin1String( defaultConnection ), bool open = true ) [static]
    Qt Assistant -- rocks!
    please, use tags [CODE] & [/CODE].

  7. #7
    Join Date
    Oct 2007
    Location
    Cracow
    Posts
    56
    Thanks
    1
    Thanked 10 Times in 10 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Disconnecting from database after using QSqlTableModel

    maybe try in on_disconnectButton_clicked first delete model and after that close and remove database

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 09:30
  2. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 17:56
  3. Replies: 2
    Last Post: 23rd February 2008, 02:58
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 15:31
  5. How to manage QSqlTableModel database operations?
    By Abk in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2007, 11:44

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.