Hello.

I have subclassed QSqlQueryModel. While reimplementing setData() I created a method which uses a QSqlQuery with some bound values to update the relevant row in the database.

Qt Code:
  1. bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
  2. {
  3. QSqlQuery query;
  4. query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
  5.  
  6. QString table_str;
  7. switch(table) {
  8. case 1:
  9. table_str = "presupuestados_cascos_y_accesorios";
  10. break;
  11. case 2:
  12. table_str = "presupuestados_complementos";
  13. break;
  14. case 3:
  15. table_str = "presupuestados_puertas_y_cajones";
  16. break;
  17. case 4:
  18. table_str = "presupuestados_electrodomesticos";
  19. break;
  20. default:
  21. return false;
  22. }
  23.  
  24. query.bindValue(":table", table_str);
  25. query.bindValue(":string", firstName);
  26. query.bindValue(":id", id);
  27.  
  28. if(query.exec())
  29. {
  30. return true;
  31. }
  32. else
  33. {
  34. qDebug() << Q_FUNC_INFO << query.lastQuery();
  35. qDebug() << Q_FUNC_INFO << query.lastError();
  36. qDebug() << Q_FUNC_INFO << query.boundValues();
  37. return false;
  38. }
  39. }
To copy to clipboard, switch view to plain text mode 

This, outputs:

Qt Code:
  1. bool EditableSqlModel::setFirstName(int, int, const QString&) "UPDATE :table SET desc = :string WHERE id = :id;"
  2. bool EditableSqlModel::setFirstName(int, int, const QString&) QSqlError(-1, "Parameter count mismatch", "")
  3. bool EditableSqlModel::setFirstName(int, int, const QString&) QMap((":id", QVariant(int, 2) ) ( ":string" , QVariant(QString, "3719000000, PUERTA ESP., 117.96") ) ( ":table" , QVariant(QString, "presupuestados_puertas_y_cajones") ) )
To copy to clipboard, switch view to plain text mode 

But, if I use this instead, it just works!

Qt Code:
  1. bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
  2. {
  3. QSqlQuery query;
  4. //query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
  5.  
  6. QString table_str;
  7. switch(table) {
  8. case 1:
  9. table_str = "presupuestados_cascos_y_accesorios";
  10. break;
  11. case 2:
  12. table_str = "presupuestados_complementos";
  13. break;
  14. case 3:
  15. table_str = "presupuestados_puertas_y_cajones";
  16. break;
  17. case 4:
  18. table_str = "presupuestados_electrodomesticos";
  19. break;
  20. default:
  21. return false;
  22. }
  23.  
  24. /* query.bindValue(":table", table_str);
  25.   query.bindValue(":string", firstName);
  26.   query.bindValue(":id", id);
  27. */
  28. if(query.exec(QString("update %1 set desc = '%2' where id = %3")
  29. .arg(table_str)
  30. .arg(firstName)
  31. .arg(id)))
  32. {
  33. return true;
  34. }
  35. else
  36. {
  37. qDebug() << Q_FUNC_INFO << query.lastQuery();
  38. qDebug() << Q_FUNC_INFO << query.lastError();
  39. qDebug() << Q_FUNC_INFO << query.boundValues();
  40. return false;
  41. }
  42. }
To copy to clipboard, switch view to plain text mode 

Noting that:

  1. The argument number, according to the debug output, is exactly the same than the number of placeholders
  2. The type of such arguments is the same in both cases
  3. The table is the same, and the database is always the default one


Can someone enlighten me on why this is failing?

Thank you.