Showing data from multiple SQLite tables



  • I have a SQLite database with 6 tables (some of them with two foreign keys). What would be the easiest solution to create a view to display data (join on multiple tables)?
    I found out, that the QSqlRelationalTableModel works well, but only with one table / one join.
    QSqlQueryModel and QSqlTableModel both dont provide the foreign key support with delegates.

    Is there a way to keep the foreign key support with the possibility of joining data from multiple tables together with simple insertation? How to insert / alter / delete data? Do I have to write querys for every single change?


  • Qt Champions 2018

    Just put your query with the joins into a QSqlQueryModel



  • @Christian-Ehrlicher

    SqlQueryModel works with Delegates and Relations?
    I dont want to write thousand lines of code just for the querys.
    And I guess you can not change a value there by clicking into the model and start typing?

    From QSqlQueryModel Doc:
    The model is read-only by default. To make it read-write, you must subclass it and reimplement setData() and flags(). Another option is to use QSqlTableModel, which provides a read-write model based on a single database table.

    So I have to code every functionality that e.g. the QSqlRelationalTableModel provides?
    That's a lot of work....


  • Qt Champions 2018

    I don't understand what you mean - you said you want to display data from different tables. So what's wrong writing a query with a join on all columns you want to display and pass t to a QSqlQueryModel?
    Editing such data will not work with any of the convenience QSql*Models in your case - you have to write your own model then.



  • @Christian-Ehrlicher

    Ok, didn't know that... Thought all Models are editable (Views obviously not)

    Edit: I can edit when I'm using a QSqlRelationalTableModel... this comes very close to what I want / need, but it only works on one table :(


  • Qt Champions 2018

    This is what I said - how should the model be able to update data in different tables when they are all joined? This is a task which can't be done generally (at least not for all cases). You have to do it on your own.



  • @Christian-Ehrlicher

    Hm ok... That's not what I wanted to hear :D
    Thank you anyway.
    I think, I will subclass QSqlQueryModel then and write functions to make the model writable...