Please Help!
I've been fighting this for a long time and would be more than greatfull someone could help...Basicly I'v got a sql table (a QTableView and a QSqlTableModel) I'v got everything working ***Except*** One thing!
I can't find any way to set the data of a field...Here's what I'm trying to do: I'v got a table with a buch of columns and rows and a text box where the user enters the tag and it appends the current date time and adds 1 to the daysAttended column

Here's the function that is executed when the user hit's enter in the textBox

Qt Code:
  1. void MainWindow::locate()
  2. {
  3. if(db.isOpen())
  4. {
  5. QString datesAttended = "";
  6. int daysAttended = 0;
  7. int daysMissed = 0;
  8. int matchingRow = 0;
  9.  
  10. QString searchTag = barcodeEdit->text().toUpper();
  11.  
  12. if(query->exec(QString("select * from persons where UID = \"%0\" LIMIT 0,1").arg(searchTag)))
  13. {
  14. if(query->isSelect() && query->next() && query->value(0).isValid())
  15. {
  16. int matchingRow = query->value(0).toInt();
  17.  
  18. daysAttended = model->record(matchingRow).value(6).toInt();
  19. datesAttended = model->record(matchingRow).value(5).toString();
  20.  
  21. if(model->setData(model->index(matchingRow,6),QVariant(daysAttended + 1)) && model->setData(model->index(matchingRow,5),QVariant(datesAttended + QDateTime::currentDateTime().toString())))
  22. {barcodeEdit->setText("");} else {
  23. QMessageBox::critical(0, qApp->tr("Error Setting Record!"),
  24. qApp->tr(" %0.\n \n"
  25. "Click Close to continue.").arg(model->lastError().text()), QMessageBox::Close);}
  26. } else {
  27. QMessageBox::critical(0, qApp->tr("Could not find an entry with the matching criticia!"),
  28. qApp->tr("Unable to find a person with the matching criticia! \n\n"
  29. "Click Close to continue."), QMessageBox::Close);}
  30. } else {
  31. QMessageBox::critical(0, qApp->tr("Error Executing Query!"),
  32. qApp->tr(" %0.\n \n"
  33. "Click Close to continue.").arg(query->lastError().text()), QMessageBox::Close);}
  34. } else {
  35. QMessageBox::critical(0, qApp->tr("Database Not Open!"),
  36. qApp->tr("You must have a database open before you can use this function!. \n\n"
  37. "Click Close to continue."), QMessageBox::Close);}
  38. }
To copy to clipboard, switch view to plain text mode 

That works and it dose what I want accept on thing...after that saving dosn't work! when I save the changes it returns the following error:

database is locked Unable to fetch row and it dosn't save!

Here's my save function

Qt Code:
  1. bool MainWindow::save()
  2. {
  3. if(db.isOpen()){
  4.  
  5. model->database().transaction();
  6. if (model->submitAll()) {
  7. model->database().commit();
  8. return true;
  9. } else {
  10. model->database().rollback();
  11. QMessageBox::warning(this, tr("Cached Table"),
  12. tr("The database reported an error: %1")
  13. .arg(model->lastError().text()));
  14. return false;
  15. }
  16. } else {
  17. QMessageBox::warning(this, tr("Database Not Open!"),
  18. tr("No database is open!"));
  19. return false;
  20. }
  21. }
To copy to clipboard, switch view to plain text mode