Results 1 to 3 of 3

Thread: QT & Sqlite problem: "database is locked"

  1. #1
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default QT & Sqlite problem: "database is locked"

    Qt 4.5.0 & Sqlite 3.6.10

    I'm using an SQLite database with two tables, EMPLOYEES & UPDATES. UPDATES is used as a queue for any changes to EMPLOYEES table.

    The idea is to scan UPDATES table and - provided that the flag for that particular row is switched on - do the following: i) make a change in EMPLOYEES table, ii) delete that entry in UPDATES table.

    Unfortunately, I keep getting "db is locked" error and I don't know how to resolve the issue. I really tried a lot, but to no avail.

    Any help would be greatly appreciated. Attaching the complete application, database and error.

    Thanks.

    Qt Code:
    1. /*
    2.  * dbtest.cpp
    3.  */
    4. #include "dbtest.h"
    5. #include <QDebug>
    6.  
    7. void DBTest::scanForUpdates() {
    8. qDebug() << "SCAN: open connections" << QSqlDatabase::connectionNames();
    9.  
    10. {
    11. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",
    12. "main_connection");
    13. db.setDatabaseName("dbtest.sqlite");
    14. qDebug() << "SCAN: open connections" << QSqlDatabase::connectionNames();
    15. if (!db.open()) {
    16. qDebug() << " ***ERROR - cannot open local database";
    17. return;
    18. }
    19.  
    20. // get all updates
    21. QSqlQuery querySelect(db);
    22. querySelect.prepare(
    23. "SELECT UPDATE_ID, PERSON_ID, NOTE, TO_CHANGE_NOW FROM UPDATES");
    24. if (!querySelect.exec()) {
    25. qDebug() << " ***ERROR - select: "
    26. << querySelect.lastError().text();
    27. return;
    28. }
    29.  
    30. while (querySelect.next()) {
    31. QSqlRecord rec = querySelect.record();
    32. int nUpdateID = rec.value("UPDATE_ID").toInt();
    33. int nEmployeeID = rec.value("PERSON_ID").toInt();
    34. QString strNote = rec.value("NOTE").toString();
    35. bool bChangeNow = rec.value("TO_CHANGE_NOW").toBool();
    36. qDebug() << "SCAN: update entry" << nUpdateID << nEmployeeID
    37. << strNote << bChangeNow;
    38.  
    39. if (bChangeNow) {
    40. bool bResult = DBTest::changeDepartment(nEmployeeID, strNote);
    41.  
    42. if (bResult) {
    43. // employee table successfully updated, delete in updates
    44. QSqlQuery queryDelete(db);
    45. queryDelete.prepare(
    46. "DELETE FROM UPDATES WHERE UPDATE_ID=:nUpdateID");
    47. queryDelete.bindValue(":nUpdateID", nUpdateID);
    48. if (!queryDelete.exec()) {
    49. qDebug() << " ***ERROR - delete:"
    50. << queryDelete.lastError().text();
    51. qDebug() << " ***query"
    52. << queryDelete.executedQuery();
    53. return;
    54. }
    55. } // end if (bResult)
    56. } // end if (bChangeNow)
    57. } // end while
    58. }
    59. QSqlDatabase::removeDatabase("main_connection");
    60. }
    61.  
    62. bool DBTest::changeDepartment(int nEmployeeID, QString strDepartment) {
    63. qDebug() << "DEPT:" << nEmployeeID << strDepartment;
    64. bool bResult = true;
    65.  
    66. {
    67. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE",
    68. "temp_connection");
    69. db.setDatabaseName("dbtest.sqlite");
    70. qDebug() << "DEPT: open connections" << QSqlDatabase::connectionNames();
    71.  
    72. if (!db.open()) {
    73. qDebug() << " ***ERROR - cannot open local database";
    74. bResult = false;
    75. }
    76.  
    77. if (bResult) {
    78. QSqlQuery queryUpdate(db);
    79. queryUpdate.prepare(
    80. "UPDATE EMPLOYEES SET DEPARTMENT=:strDepartment "
    81. "WHERE PERSON_ID=:nEmployeeID");
    82. queryUpdate.bindValue(":strDepartment", strDepartment);
    83. queryUpdate.bindValue(":nEmployeeID", nEmployeeID);
    84. if (!queryUpdate.exec()) {
    85. qDebug() << " ***ERROR - update: "
    86. << queryUpdate.lastError().text();
    87. qDebug() << " ***query" << queryUpdate.executedQuery();
    88. bResult = false;
    89. }
    90. }
    91. }
    92. QSqlDatabase::removeDatabase("temp_connection");
    93.  
    94. return bResult;
    95. }
    To copy to clipboard, switch view to plain text mode 
    Attached Files Attached Files

  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: QT & Sqlite problem: "database is locked"

    Don't open a new database connection in changeDepartment(), just use:
    Qt Code:
    1. bool DBTest::changeDepartment(int nEmployeeID, QString strDepartment) {
    2. qDebug() << "DEPT:" << nEmployeeID << strDepartment;
    3. bool bResult = true;
    4. QSqlDatabase db = QSqlDatabase::database("main_connection"); // it's already open!
    5. QSqlQuery queryUpdate(db);
    6. queryUpdate.prepare(
    7. "UPDATE EMPLOYEES SET DEPARTMENT=:strDepartment "
    8. "WHERE PERSON_ID=:nEmployeeID");
    9. queryUpdate.bindValue(":strDepartment", strDepartment);
    10. queryUpdate.bindValue(":nEmployeeID", nEmployeeID);
    11. if (!queryUpdate.exec()) {
    12. qDebug() << " ***ERROR - update: "
    13. << queryUpdate.lastError().text();
    14. qDebug() << " ***query" << queryUpdate.executedQuery();
    15. bResult = false;
    16. }
    17.  
    18. return bResult;
    19. }
    To copy to clipboard, switch view to plain text mode 

  3. The following user says thank you to Lykurg for this useful post:

    xfurrier (15th July 2009)

  4. #3
    Join Date
    Jan 2009
    Posts
    20
    Thanks
    7
    Qt products
    Qt4
    Platforms
    MacOS X

    Default Re: QT & Sqlite problem: "database is locked"

    Quote Originally Posted by Lykurg View Post
    Don't open a new database connection in changeDepartment(), just use:
    Qt Code:
    1. bool DBTest::changeDepartment(int nEmployeeID, QString strDepartment) {
    2. qDebug() << "DEPT:" << nEmployeeID << strDepartment;
    3. bool bResult = true;
    4. QSqlDatabase db = QSqlDatabase::database("main_connection"); // it's already open!
    5. QSqlQuery queryUpdate(db);
    6. ...
    To copy to clipboard, switch view to plain text mode 
    Absolutely right - it's amazing how one can`t see the forest for the trees. The solution is so obvious and so simple - once you know it. Thanks.

Similar Threads

  1. Weird problem: multithread QT app kills my linux
    By Ishark in forum Qt Programming
    Replies: 2
    Last Post: 8th August 2008, 09:12
  2. Problem with A SQLite Query
    By maveric in forum Qt Programming
    Replies: 1
    Last Post: 24th June 2008, 11:15
  3. SQLITE QTableView scrollTo problem
    By vogeljh in forum Newbie
    Replies: 7
    Last Post: 16th April 2008, 19:45
  4. SQLite Problem
    By natbobc in forum Qt Programming
    Replies: 5
    Last Post: 8th November 2007, 15:31
  5. Problem with SqLite and Qt
    By ad5xj in forum Newbie
    Replies: 26
    Last Post: 5th June 2007, 01:53

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.