QML - Error: database table is locked Unable to fetch row



  • Hi Guys,

    I have encountered a frustratring problem with my application that I cannot explain. Basically, when I start the application, I am able to do anything involving the database BESIDES dropping tables (reproducable upon restart). If I try to drop a table, I get "Error: database table is locked Unable to fetch row"... but after any three transactions (select, insert etc..) I am able to drop a table. The only documentation I could find about using a database from QML was http://doc.qt.nokia.com/4.7-snapshot/qdeclarativeglobalobject.html, and it is not very detailed.

    "This":https://gitorious.org/harmttan/scenic/blobs/master/qml/main.qml is my main QML file. On line 14, I select * from my main table, "wallpapers" and get the results just fine in a Component.onCompleted field. The code is in a stateless javascript library (.pragma) and is located "here":https://gitorious.org/harmttan/scenic/blobs/master/qml/Database.js, but they all look pretty much like this:

    @var db = openDatabaseSync('scenic', '1.0', 'Wallpaper database', 100000); //global

    function getAllPacks (){

    var returnArray;
    db.transaction(
                function(tx){
                    tx.executeSql('CREATE TABLE IF NOT EXISTS wallpapers(pack TEXT, name TEXT, path TEXT, key TEXT UNIQUE)');
                    returnArray = tx.executeSql("select distinct pack from wallpapers")
                }
                )
    return returnArray
    

    }@

    I really have no idea why it complains about DB locking and I am not sure where to look for help... Any guidance would be greatly appreciated! This is the only thing stopping me from publishing.



  • bumping to hopefully resolve...



  • Hi externc,
    I am facing the same problem, how did you solved that?



  • Unfortunately, I was never able to resolve this. I ended up moving my database code to a QSqlQueryModel where I have more control. I suspect the problem was a Qt bug where the database connection was not properly closed.



  • Not sure but try to convert the

    @
    var db = openDatabaseSync('scenic', '1.0', 'Wallpaper database', 100000);
    @

    into a function and call everytime you wanna do transaction() with the database.



  • Thanks externc, that's what I am doing now, if you can share your class I will be very appreciated.
    @Dickson: I was doing that and it doesn't work, what we need is a close function so that we can close the database every time a write access is done so that we ensure that the DB isn't locked, but the Offline Storage API doesn't offer that so we are forced to implemented with c++.



  • No problem. The specific file you want is https://gitorious.org/scenic/scenic/blobs/master/scenicdbmodel.h. All of the source code is there as well in case you wanted to have a look at it, but I think https://gitorious.org/rewards-manager would be more helpful; its a similar app of mine.

    In the second link, I implement the database code in c++, but without using the QSqlQueryModel. I also use the objects that I create from the database in the QML code, which I didn't know how to do when I created Scenic.



  • Thank you externc, it was very useful.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.