Important: Please read the Qt Code of Conduct -

Is QSqlQueryModel an alternative to QSqlQuery? - Qt

  • Hi,

    I am using QSqlQuery in my Qt application. During the application run, i sometimes get error which says "Unable to fetch row. Database locked.".

    I found somebody suggesting QSqlQueryModel as an alternate to QSqlQuery. Is it so because the documentation says that QSqlQueryModel provides a read only data model for sql results sets which means this can be used only to retrieve data from the database. On the other hand QSqlQuery can be used to execute all kinds of sql queries.

    What do you have to say on this.


  • Lifetime Qt Champion


    What kind of database are you accessing ? Are you accessing it alone ?

    QSqlQueryModel vs QSqlQuery is more a question of what you are doing in your application

  • Sorry forgot to mention that. Its a sqlite database. I am using QSqlDatabase API for this. In the application I would use most of the database operations like create, add, delete, update.

  • Lifetime Qt Champion

    Are you using multi threading in your application ?

    You can also have a look at QSqlTableModel for this kind of activity

  • Yes I am using multithreading in the app. So are you suspecting that "unable to fetch row" error is related to accessing the database from two different threads is it?

    But this scenario might happen even if I use QSqlTableModel. So does that mean that there is something wrong in the way I am using multithreading in my application is it? Because I have also observed that my application hangs frequently and whenever it hangs, there is some DB call going on the in background which keeps returning "unable to fetch row".

    Please suggest what is the better way to do it.


  • Lifetime Qt Champion

    Then I guess that you are using sqlite for your database ? "this": might help you

Log in to reply