Results 1 to 7 of 7

Thread: Disconnecting from database after using QSqlTableModel

Threaded View

Previous Post Previous Post   Next Post Next Post
  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 12:48. Reason: added source files as attachment

Similar Threads

  1. Threads and database connection
    By probine in forum Qt Programming
    Replies: 9
    Last Post: 7th August 2013, 08:30
  2. Multiple database connections
    By cyberboy in forum Qt Programming
    Replies: 3
    Last Post: 30th March 2008, 16:56
  3. Replies: 2
    Last Post: 23rd February 2008, 01:58
  4. Database Master-Detail Entry Form
    By Phan Sin Tian in forum Newbie
    Replies: 4
    Last Post: 3rd February 2008, 14:31
  5. How to manage QSqlTableModel database operations?
    By Abk in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2007, 10: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.