Results 1 to 3 of 3

Thread: Not able to insertRecord with QSqlRelationalTableModel

  1. #1
    Join Date
    Jun 2017
    Location
    Portland OR
    Posts
    2
    Qt products
    Qt5

    Question Not able to insertRecord with QSqlRelationalTableModel

    I am trying to teach myself database features, and I have come into a problem. I have a relational table model for a mysql database table, but insertRecord fails. I am sure I am not understanding something, probably at a fundamental level. I was hoping some of you more experienced users could help. Here is what we are dealing with:

    MySql 5.7 Tables:
    Qt Code:
    1. DESCRIBE games;
    2. +-------+------------------+------+-----+---------+----------------+
    3. | Field | Type | Null | Key | Default | Extra |
    4. +-------+------------------+------+-----+---------+----------------+
    5. | name | varchar(16) | NO | UNI | NULL | |
    6. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    7. +-------+------------------+------+-----+---------+----------------+
    8.  
    9. DESCRIBE drawings;
    10. +---------+------------------+------+-----+---------+----------------+
    11. | Field | Type | Null | Key | Default | Extra |
    12. +---------+------------------+------+-----+---------+----------------+
    13. | date | date | NO | | NULL | |
    14. | game_id | int(11) | NO | | NULL | |
    15. | id | int(10) unsigned | NO | PRI | NULL | auto_increment |
    16. +---------+------------------+------+-----+---------+----------------+
    To copy to clipboard, switch view to plain text mode 

    C++
    Qt Code:
    1. Game::Game(QTabWidget *parent, QString name) :
    2. QTabWidget(parent),
    3. ui(new Ui::Game)
    4. {
    5. this->db = QSqlDatabase::database("LC");
    6.  
    7. this->dbGames = new QSqlRelationalTableModel(this, db);
    8. this->dbDrawings = new QSqlRelationalTableModel(this ,db);
    9. this->dbRecords = new QSqlRelationalTableModel(this, db);
    10. this->dbRules = new QSqlRelationalTableModel(this, db);
    11. this->dbTypes = new QSqlRelationalTableModel(this, db);
    12.  
    13. this->dbGames->setTable("games");
    14. this->dbDrawings->setTable("drawings");
    15. this->dbRecords->setTable("records");
    16. this->dbRules->setTable("rules");
    17. this->dbTypes->setTable("types");
    18.  
    19. this->dbGames->setEditStrategy(QSqlTableModel::OnFieldChange);
    20. this->dbDrawings->setEditStrategy(QSqlTableModel::OnFieldChange);
    21. this->dbRecords->setEditStrategy(QSqlTableModel::OnFieldChange);
    22. this->dbRules->setEditStrategy(QSqlTableModel::OnFieldChange);
    23. this->dbTypes->setEditStrategy(QSqlTableModel::OnFieldChange);
    24.  
    25. this->dbDrawings->setRelation(this->dbDrawings->fieldIndex("game_id"), QSqlRelation("games", "id", "name"));
    26. this->dbRecords->setRelation(this->dbRecords->fieldIndex("drawing_id"), QSqlRelation("drawings", "id", "date"));
    27. this->dbRecords->setRelation(this->dbRecords->fieldIndex("type_id"), QSqlRelation("types", "id", "type"));
    28. this->dbRules->setRelation(this->dbRules->fieldIndex("type_id"), QSqlRelation("types", "id", "type"));
    29. this->dbRules->setRelation(this->dbRules->fieldIndex("game_id"), QSqlRelation("games", "id", "name"));
    30.  
    31. this->dbGames->select();
    32. this->dbDrawings->select();
    33. this->dbRecords->select();
    34. this->dbRules->select();
    35. this->dbTypes->select();
    To copy to clipboard, switch view to plain text mode 

    and later in the same class:
    Qt Code:
    1. int Game::dbaddDrawing(QString dateString)
    2. {
    3. this->dbDrawings->setFilter("");
    4. this->dbDrawings->select();
    5.  
    6. QDate recordDate = QDate::fromString(dateString, this->dateFormat);
    7.  
    8. QSqlRecord drawing = this->dbDrawings->record();
    9.  
    10. drawing.setValue(this->dbDrawings->fieldIndex("date"), recordDate);
    11. drawing.setValue(this->dbDrawings->fieldIndex("name"), QVariant(this->gameName));
    12. drawing.setGenerated(this->dbDrawings->fieldIndex("id"), false);
    13.  
    14. if (!this->dbDrawings->insertRecord(-1, drawing)) //this seems to be where the problem is.
    15. {
    16. console("Drawing Input failure!");
    17. }
    18.  
    19. int returnVal = this->dbDrawings->query().lastInsertId().toInt();
    20.  
    21. return returnVal;
    22. }
    To copy to clipboard, switch view to plain text mode 

    If I setValue for game_id instead of the relational tables show field (name), the outcome does not change.

    My biggest thought has been to look at the insert query and see if that gives hints to where the failure is, but looking at
    Qt Code:
    1. this->dbDrawings->query().lastQuery();
    To copy to clipboard, switch view to plain text mode 
    it always seems to show select queries and never the insert. How can I look at the insert query? is it not showing the insert query because it is never passed? Recommendations for how I can check?

    Also, in the docs for QSqlRelationalTableModel it says "If you use a read-write QSqlRelationalTableModel, you probably..." That first part about read-write, what is the deal with that? Is that in reference to the permissions of the db user on the db, or is there a property that I am not seeing in the class that sets permissions?

    **Deep breath** I have been at this for a couple of weeks now, while I can operate the database in QSqlTableModel's I can not seem to insert to it's relational counterpart, and I am out of ideas.

    Please remember that I am very new (less than a year) to Qt and C++, and not very proficient in MySQL, so please be kind. Any help is greatly appreciated.

    THANKS!

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,536
    Thanked 284 Times in 279 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Not able to insertRecord with QSqlRelationalTableModel

    Take a look to MySQL logs.

  3. #3
    Join Date
    Jun 2017
    Location
    Portland OR
    Posts
    2
    Qt products
    Qt5

    Default Re: Not able to insertRecord with QSqlRelationalTableModel

    Thank you for the suggestion, Lesiok. I had already looked, but I figured, what the heck, one more time to be sure. Unfortunately, when I look through the MySQL logs there is nothing pointing to the problem. My guess at this point, is that it fails in the Qt Sql class somewhere (most likely because I screwed something up in my code) and never even gets presented to MySQL. I think I am going to revert to the QSqlTableModel, and process relations manually for now... unless anyone has any other suggestions? Is this the best way to process relational tables? Thanks again, in advance.

Similar Threads

  1. QSqlRelationalTableModel & QTableView
    By eaglo in forum Qt Programming
    Replies: 0
    Last Post: 16th December 2011, 21:21
  2. QSqlTableModel and insertRecord()
    By Hostel in forum Newbie
    Replies: 4
    Last Post: 6th September 2011, 01:42
  3. when to use QSqlRelationalTableModel?
    By babygal in forum Qt Programming
    Replies: 1
    Last Post: 5th October 2010, 10:19
  4. QSqlRelationalTableModel
    By marvin in forum Newbie
    Replies: 2
    Last Post: 19th November 2008, 22:29
  5. QSqlRelationalTableModel
    By jjay in forum Qt Programming
    Replies: 1
    Last Post: 20th February 2008, 16:20

Tags for this Thread

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.