Results 1 to 6 of 6

Thread: qsqlquery database locked with SQLITE

  1. #1
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default qsqlquery database locked with SQLITE

    I'm using Qt5 with an QSqlDatabase with QSQLITE. I initialize the Database with
    Qt Code:
    1. db = QSqlDatabase::addDatabase("QSQLITE");
    2. db.setDatabaseName("test.db");
    3. if (!db.open()) { ...}
    To copy to clipboard, switch view to plain text mode 

    Two instances of the program or running on the same machine therefore both or using the same test.db database file. The initialization of the database works fine, but when executing

    Qt Code:
    1. bool DataStorageHandler::executeStmt(QString stmt) {
    2. QSqlQuery qry(this->db);
    3. qry.prepare(stmt);
    4.  
    5. if (!qry.exec()) {
    6. qDebug() << stmt;
    7. this->dbError = qry.lastError().text();
    8. return false;
    9. } else
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 

    Both instances at this point are executing an INSERT-Stmt but one of them gets an error

    database is locked Unable to fetch row

    I don't understand why? Shouldn't sqlite handle different accesses to the DB?

  2. #2
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery database locked with SQLITE

    The instance that writes first will likely employ some file locking to ensure data integrity.
    If the second instance's write happens at the same time, it will of course fail and you'll have to retry later.

    On Windows that will of course always fail, since it has single-process exlusive file locking by default.

    Cheers,
    _

  3. #3
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: qsqlquery database locked with SQLITE

    Ok what would be the right approach to solve this problem?
    Seperate .db file? But then how to name them since it is the same application running on the same machine and when starting the next time the application should access the right file
    Or is there an option to wait till the first process finished its access and then execute the second INSERT-Stmt?

  4. #4
    Join Date
    Jan 2006
    Location
    Graz, Austria
    Posts
    8,416
    Thanks
    37
    Thanked 1,544 Times in 1,494 Posts
    Qt products
    Qt3 Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: qsqlquery database locked with SQLITE

    There are a couple of options:

    1) Close the database connection when you are done with a query. Will still require some retry loop
    2) Communicate between the instances so each knows when it can access the database
    3) Have one process for accessing the database, at least for writing, which the application instances communicate with
    4) Use a database engine that implements (3)

    Cheers,
    _

  5. #5
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: qsqlquery database locked with SQLITE

    Sqlite will lock the file during write transactions and free it when the changes are committed. Assuming the insert is a whole transaction, try calling transaction() on the database before executing the insert and commit() (or rollback()) afterward.

  6. The following user says thank you to ChrisW67 for this useful post:

    Cerberus (11th October 2015)

  7. #6
    Join Date
    Sep 2015
    Posts
    23
    Thanks
    1

    Default Re: qsqlquery database locked with SQLITE

    Using transactions worked, thx for the help

Similar Threads

  1. QSqlQuery update query not working using SQLite
    By Cyrebo in forum Qt Programming
    Replies: 2
    Last Post: 29th March 2013, 00:33
  2. QSqlite "database is locked"
    By flob in forum Qt Programming
    Replies: 6
    Last Post: 14th December 2009, 14:18
  3. QT & Sqlite problem: "database is locked"
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2009, 07:06
  4. QSqlQuery, bindValue and Sqlite
    By cydside in forum Qt Programming
    Replies: 4
    Last Post: 5th April 2009, 16:53
  5. Removing locked attribute on Mac locked files?
    By gfunk in forum Qt Programming
    Replies: 1
    Last Post: 26th October 2007, 22:40

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.