Results 1 to 2 of 2

Thread: Getting the Primary Key value for a new record in mapper

  1. #1
    Join Date
    Jun 2012
    Posts
    33
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Getting the Primary Key value for a new record in mapper

    I have a qdatawidgetmapper which I use to browse through data from a specific table in MySQL.

    Qt Code:
    1. // Setup the model to use in the mapper
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("Customers");
    4. model->select();
    5.  
    6. // Setup the mapper for the product widgets
    7. mapper = new QDataWidgetMapper(this);
    8. mapper->setModel(model);
    9. mapper->addMapping(ui->cNumLineEdit, 0);
    10. mapper->addMapping(ui->nameLineEdit, 1);
    11. mapper->addMapping(ui->frequencyComboBox, 2, "currentIndex");
    12. mapper->addMapping(ui->priorityComboBox, 3, "currentIndex");
    13. mapper->addMapping(ui->typeComboBox, 4, "currentIndex");
    14. mapper->addMapping(ui->address1LineEdit, 5);
    15. mapper->addMapping(ui->address2LineEdit,6);
    16. mapper->addMapping(ui->cityLineEdit,7);
    17. mapper->addMapping(ui->postalCodeLineEdit,8);
    18. mapper->addMapping(ui->telephoneLineEdit,9);
    To copy to clipboard, switch view to plain text mode 

    I try to create a new record and it creates normally but the primary key is an autoincrement number provided from MySQL. I would like to refresh the record to include the number in the form. I believe that not having the value produces errors when trying to save data or add other data which are related to the new record.

    Qt Code:
    1. void Customers::on_newPushButton_clicked()
    2. {
    3. // Create a new item and clear all widgets
    4. QSqlRecord sqlRecord1;
    5. model->insertRecord(-1,sqlRecord1);
    6.  
    7. ui->cNumLineEdit->clear();
    8. ui->nameLineEdit->clear();
    9. ui->address1LineEdit->clear();
    10. ui->address2LineEdit->clear();
    11. ui->cityLineEdit->clear();
    12. ui->postalCodeLineEdit->clear();
    13. ui->telephoneLineEdit->clear();
    14. mapper->toLast();
    15. }
    To copy to clipboard, switch view to plain text mode 

    Can anyone tell me if I am doing it correctly or if I need to insert the record and retrieve it in a different way?

    Thanks,

    Pericles

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: Getting the Primary Key value for a new record in mapper

    Until the model actually tries to put the new record into the database there is no MySQL allocated number to access. When it does that is dependent on the update strategy. The submit() or submitAll() calls can force writing to the database, but this will fail if other relevant constraints are not met.

    You may be able to mark the auto-numbered id column, see QSqlRecord::setGenerated(), to stop the Qt code trying to insert NULL into that column (assuming that's an error you are seeing).

    IMHO this sort of surrogate primary key should never have a meaning to the user and thus never need to be displayed except, perhaps, for debugging purposes.

Similar Threads

  1. Replies: 2
    Last Post: 25th June 2012, 10:54
  2. Replies: 7
    Last Post: 27th November 2010, 15:55
  3. Replies: 3
    Last Post: 26th March 2010, 04:32
  4. mapper stop after upgrade to 4.4.3
    By SunnySan in forum Qt Programming
    Replies: 6
    Last Post: 6th November 2008, 11:50
  5. Possible signal mapper problem
    By MarkoSan in forum Qt Programming
    Replies: 13
    Last Post: 25th January 2008, 13:11

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.