Results 1 to 9 of 9

Thread: Way to load all of data from 2 tables into one QTableView?

  1. #1
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Way to load all of data from 2 tables into one QTableView?

    I have 2 tables; between them have a one-to-many relationship. They have a one-to-many relationship with a language table.

    So, I want load all of data in 2 tables above into one Table View, edit and add new record to them. What way I can do? Help me….

  2. #2
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Way to load all of data from 2 tables into one QTableView?

    Create a database view and base your model on the view.

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

    Kevin Hoang (2nd April 2010)

  4. #3
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Way to load all of data from 2 tables into one QTableView?

    Thanks waynew!

    Does QT support to create a view tables?

  5. #4
    Join Date
    May 2009
    Location
    USA
    Posts
    300
    Thanks
    82
    Thanked 11 Times in 11 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Way to load all of data from 2 tables into one QTableView?

    I don't know what database you are using, but the general syntax would be:

    "CREATE view my_view col_1, col_2 as SELECT col1, col2 from table1, table2, where table1.<id_col> = table2.<id_col>"

    Then set your model table to my_view.

    where table1 contains columns col1 and id_col and table2 contains columns col2 and id_col.
    You must join the tables on columns that will hold the same data.
    For Sqlite, see here: http://www.hwaci.com/sw/sqlite/lang.html and here: http://www.hwaci.com/sw/sqlite/lang_createview.html

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

    Kevin Hoang (3rd April 2010)

  7. #5
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Way to load all of data from 2 tables into one QTableView?

    I'm using SQLite.

    Follow 2 links you recommended, I get new problem: "Views are read-only in SQLite. I cannot DELETE, INSERT, or UPDATE a view"

    what next I should do with this task?

  8. #6
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Way to load all of data from 2 tables into one QTableView?

    About SQLite:
    You cannot DELETE, INSERT, or UPDATE a view. Views are read-only in SQLite. However, in many cases you can use an INSTEAD OF trigger on the view to accomplish the same thing
    Look here http://www.hwaci.com/sw/sqlite/lang_...ead_of_trigger
    It should be something like that (data, text are two tables, and one view you create )
    Qt Code:
    1. CREATE TRIGGER view_insert INSTEAD OF INSERT ON view
    2. BEGIN
    3. INSERT INTO data (....) VALUES (....);
    4. INSERT INTO text (....) VALUES (....);
    5. END;
    To copy to clipboard, switch view to plain text mode 

    You'll find a link here, it's in french, but it should be useful http://www.developpez.net/forums/d79...-tables-liees/
    Last edited by toutarrive; 2nd April 2010 at 17:26. Reason: Adding link

  9. The following user says thank you to toutarrive for this useful post:

    Kevin Hoang (3rd April 2010)

  10. #7
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Way to load all of data from 2 tables into one QTableView?

    Thanks to toutarrive: I got it! The QT seemed doesn't support to create a view and trigger, I have to make query for this.

    I have another question about this: How may I insert, update, delete a table has many-to-many relationship on TableView? Example: I want to add or edit a product. The product table has many-to-many relationship with collection table and language table.

  11. #8
    Join Date
    Mar 2008
    Location
    France
    Posts
    149
    Thanks
    2
    Thanked 21 Times in 21 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Way to load all of data from 2 tables into one QTableView?

    You'll need a junction table. See SQLite documentation

  12. #9
    Join Date
    Mar 2010
    Posts
    68
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows Symbian S60

    Default Re: Way to load all of data from 2 tables into one QTableView?

    Yes, I see.
    Does QTableView and QSql...Model support to do this?

Similar Threads

  1. Get data from a QTableView
    By graciano in forum Qt Programming
    Replies: 3
    Last Post: 19th January 2010, 15:31
  2. QTableView does not show data
    By waynew in forum Newbie
    Replies: 4
    Last Post: 11th December 2009, 23:38
  3. Replies: 6
    Last Post: 26th March 2009, 05:45
  4. QTableView - data() - item
    By starcontrol in forum Qt Programming
    Replies: 1
    Last Post: 11th April 2008, 14:41
  5. Data not being added to my QTableView?
    By steg90 in forum Qt Programming
    Replies: 1
    Last Post: 20th November 2007, 14:20

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.