Results 1 to 1 of 1

Thread: Problem with creating records in a MYSQL database

  1. #1
    Join Date
    Jun 2012
    Qt products

    Default Re: Problem with creating records in a MYSQL database

    I am having a lot of trouble finding out a problem with MYSQL and QT. If anyone can help it will be much appreciated.

    I have the following code to initialize a model with a MYSQL table

    Qt Code:
    1. // Setup the model to use in the mapper
    2. model = new QSqlRelationalTableModel(this);
    3. model->setTable("Orders");
    4. model->setRelation(5, QSqlRelation("customers", "CustomerNumber", "LastName"));
    5. model->setRelation(6, QSqlRelation("employee", "EmployeeNumber", "LastName"));
    6. model->select();
    7. Qt::SortOrder order = Qt::AscendingOrder;
    8. model->sort(0, order);
    10. // Setup the mapper for the order widgets
    11. mapper = new QDataWidgetMapper(this);
    12. mapper->setSubmitPolicy(QDataWidgetMapper::AutoSubmit);
    13. mapper->setModel(model);
    14. mapper->setItemDelegate(new QSqlRelationalDelegate(model));
    15. mapper->addMapping(ui->idLineEdit, 0);
    16. mapper->addMapping(ui->fullfilledCheckBox,1);
    17. mapper->addMapping(ui->fullfilledDateEdit, 2);
    18. mapper->addMapping(ui->orderDateEdit, 3);
    19. mapper->addMapping(ui->dueDateEdit, 4);
    20. mapper->addMapping(ui->customerLineEdit, 5);
    21. mapper->addMapping(ui->employeeLineEdit, 6);
    To copy to clipboard, switch view to plain text mode 

    This code creates a model with an Orders table in MYSQL and then uses QDataWidgetMapper to map the fields to the respective widgets in the form. The CustomerLineEdit and the EmployeeLineEdit get their values from another form where you can select the name of the Customer and it inserts the value in the line edit.

    I create a new order using the following:

    Qt Code:
    1. void addOrder::on_newPushButton_clicked()
    2. {
    3. if (!model->submitAll())
    4. qDebug() << model->lastError();
    5. if (!itemModel->submitAll())
    6. qDebug() << itemModel->lastError();
    7. // Create a new item and clear all widgets
    8. QSqlRecord sqlRecord1;
    9. model->insertRecord(-1,sqlRecord1);
    10. mapper->toLast();
    11. clearFields();
    12. updateButtons(mapper->currentIndex());
    13. }
    To copy to clipboard, switch view to plain text mode 

    I save any work that I was doing before clicking New and then insert a new blank record in the model and move the mapper to the last item. I then clear the fields from any values they might have and change the Previous and Next buttons.

    When I click on the Save button the following code is run:

    Qt Code:
    1. void addOrder::on_savePushButton_clicked()
    2. {
    3. int currIndex = mapper->currentIndex();
    4. mapper->submit();
    5. // Submit all changes to the database
    6. if (!model->submitAll())
    7. qDebug() << model->lastError();
    8. if (!itemModel->submitAll())
    9. qDebug() << itemModel->lastError();
    10. ui->savePushButton->setEnabled(false);
    11. mapper->setCurrentIndex(currIndex);
    12. }
    To copy to clipboard, switch view to plain text mode 

    When I run the program I get the following error. This is because the mapper has an empty value for the OrderID which should be AutoIncremented in MYSQL.

    QSqlError(1366, "QMYSQL3: Unable to execute statement", "Incorrect integer value: '' for column 'OrderID' at row 1")

    I have another form for another table and this works fine so MYSQL is working.

    Can anyone spot any problems in this code?



    Added after 9 minutes:

    If I remove lines 4,5,14 which are responsible for setting the relationships between the orders table and the customer and employee tables then the whole thing works fine.

    Any ideas on how I can incorporate the relationships correctly or why it destroys my saving?

    Added after 26 minutes:

    Once again after a little further debugging I found another clue.

    I put back lines 4,5,14 and run the code.

    If I type the numbers of the customerid and the employeeid and save it works fine. It even shows the names of the customer and employee. If I use the Find form to put the number in the field it does not work.

    The code I use in the Find is as follows:

    Qt Code:
    1. void addOrder::on_findEmployeePushButton_clicked()
    2. {
    3. // Find an Employee
    4. // This will be deprecated when the final program is done because it will get the value from the logged in user
    5. FindEmployee *find = new FindEmployee();
    6. if (find->exec())
    7. ui->employeeLineEdit->setText(QString::number(find->getIndex()));
    8. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. void addOrder::on_findCustomerPushButton_clicked()
    2. {
    3. // Find a Customer
    4. findCustomer *find = new findCustomer();
    5. if (find->exec())
    6. ui->customerLineEdit->setText(QString::number(find->getIndex()));
    7. }
    To copy to clipboard, switch view to plain text mode 

    Added after 43 minutes:

    Found it.

    I added the command ui->employeeLineEdit->setFocus(); to force the mapper to autosubmit when it loses focus.

    Everything works fine now.

    Last edited by pcheng; 31st July 2012 at 12:10.

Similar Threads

  1. Problem During fatching records from QMAP container
    By lekhrajdeshmukh in forum Qt Programming
    Replies: 2
    Last Post: 27th October 2011, 14:12
  2. Replies: 0
    Last Post: 15th October 2011, 15:25
  3. problem creating a mysql database
    By TonyB in forum Qt Programming
    Replies: 10
    Last Post: 23rd July 2010, 16:39
  4. Replies: 2
    Last Post: 7th December 2009, 15:15
  5. Problem Wtih Connecting MySQL Database on Windows
    By dummystories in forum Installation and Deployment
    Replies: 25
    Last Post: 29th April 2009, 12:12


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.