Results 1 to 7 of 7

Thread: QSqlite "database is locked"

  1. #1
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Question QSqlite "database is locked"

    Hi All,

    I have a database class that accesses a local file using Qt's Sqlite3 drivers. The database class is implemented as a Singleton model and is accessible in that manner by the rest of the application, and thus guaranteeing that only one process is accessing the database at any given time.

    My problem is that when I use a QSqlQueryModel and call its setQuery() method, I get a QSqlError of "database is locked." This error is thrown on my FIRST attempt to query the database, which happens to be a SELECT statement.

    One more caveat is that this code works perfectly well under Linux (Ubuntu) and Mac OS X, but fails with the above error on an embedded ARM920t platform.

    On the same target hardware, I have been able to successfully run a few simple "Hello World" style applications, so I know that at least the basic cross-compiled libraries are correct.

    Cross compiled under Ubuntu, using the Qt 4.5.3 embedded source.

    Thanks in advance.

  2. #2
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlite "database is locked"

    Hi. I've got the same issue a couple of months ago. Let me check in code how I solved that. I'll promise to post the workaround right after I remember that one. Also the description of this project is on SQLite site. So you can check that there too.

  3. #3
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlite "database is locked"

    If you could, that would be GREATLY appreciated! Thanks!

  4. #4
    Join Date
    Apr 2008
    Posts
    196
    Thanked 8 Times in 6 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Wiki edits
    1

    Default Re: QSqlite "database is locked"

    @Tanuki-no Torigava: Thank you very much. Did you mean this site?

    Best Regards
    NoRulez

  5. #5
    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: QSqlite "database is locked"

    Do you use threads which access the database? If not, no singleton pattern is really needed and I'd skip it, since the global, static QSqlDatabase::database() is good enough.

    And can you show us some code of your singleton database class and how do you access the database form outside that class. So we could eventually see an error. (Even if it is strange that it works on Linux and Mac OSX...)

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

    flob (14th December 2009)

  7. #6
    Join Date
    Sep 2009
    Location
    Tashkent, Uzbekistan
    Posts
    107
    Thanks
    1
    Thanked 4 Times in 4 Posts
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlite "database is locked"

    Ok. Here it is the details. I'm using that class from the threads. So, what I actually did was:
    - Use pragmas to change the default SQLite behaviour. See details here;

    - Use transactions (See QSqlDatabase::transaction () and QSqlDatabase::commit()).That let me don't really lock the write to the database for a long time;

    - Play with QSQLITE_BUSY_TIMEOUT.

    My code sample
    Qt Code:
    1. const static char* SESSION_NAME = "MySession";
    2.  
    3. static const char* s_db_pragma[] = {
    4. "synchronous=OFF",
    5. "count_changes=OFF",
    6. "temp_store=MEMORY",
    7. NULL
    8. };
    9.  
    10. // Check if SQLite driver is available
    11. if (!QSqlDatabase::isDriverAvailable("QSQLITE"))
    12. {
    13. qDebug << "SQLite 3.x driver unavailabe. Check Qt build!";
    14. return;
    15. }
    16.  
    17. // Note, db_path is a path to your SQLite database
    18. QSqlDatabase m_db = QSqlDatabase::addDatabase("QSQLITE", SESSION_NAME);
    19. m_db.setDatabaseName(db_path);
    20. if (!m_db.isValid() || !m_db.open())
    21. {
    22. qDebug << "Unable to open SQLite 3.x database from " << db_path;
    23. return;
    24. }
    25.  
    26. // Enable SQLite pragmas
    27. int i = 0;
    28. do {
    29. QString sql = QString("PRAGMA %1;").arg(s_db_pragma[i]);
    30. QSqlQuery query(m_db);
    31. query.prepare(sql);
    32. query.exec();
    33.  
    34. if (query.lastError().isValid())
    35. {
    36. qDebug << "Query: \"" << query.lastQuery()
    37. << "\"; Error: \"" << query.lastError().text() << "\"."
    38. return;
    39. }
    40. } while (s_db_pragma[++i]);
    41.  
    42. // Continue from here with your queries
    To copy to clipboard, switch view to plain text mode 

    Best regards,

    -- Tanuki

    P.S. It's quite easy to use transactions. Place QSqlDatabase::transaction () before query block and use QSqlDatabase::commit() after it. Also you can use QSqlDatabase::rollback(). Since SQLite 3.x this approach is very usefull and works good.
    Last edited by Tanuki-no Torigava; 12th December 2009 at 01:51.

  8. The following user says thank you to Tanuki-no Torigava for this useful post:

    flob (14th December 2009)

  9. #7
    Join Date
    Nov 2009
    Posts
    10
    Thanks
    2
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlite "database is locked"

    Hi All!

    Thank you all very much for your responses!

    I believe I tracked down the issue:

    The resolution lay in an incorrect configuration within the file system of my target machine. Apparently, I had to select the configuration that allowed for "standard file locking behavior." Once I selected that option and recompiled, and placed the new image onto the ARM board, I got past my database calls.

    I am now seeing a segmentation fault after the main GUI is initialized, on the QApplication.exec call from main. The QWindow gets past the show() call, but then seg faults before anything appears on the display. Oi. Guess it's time to post another thread...

    Thanks again!

Similar Threads

  1. QT & Sqlite problem: "database is locked"
    By xfurrier in forum Qt Programming
    Replies: 2
    Last Post: 17th July 2009, 08:06

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.