i92guboj
20th August 2013, 17:28
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.
bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
{
QSqlQuery query;
query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
QString table_str;
switch(table) {
case 1:
table_str = "presupuestados_cascos_y_accesorios";
break;
case 2:
table_str = "presupuestados_complementos";
break;
case 3:
table_str = "presupuestados_puertas_y_cajones";
break;
case 4:
table_str = "presupuestados_electrodomesticos";
break;
default:
return false;
}
query.bindValue(":table", table_str);
query.bindValue(":string", firstName);
query.bindValue(":id", id);
if(query.exec())
{
return true;
}
else
{
qDebug() << Q_FUNC_INFO << query.lastQuery();
qDebug() << Q_FUNC_INFO << query.lastError();
qDebug() << Q_FUNC_INFO << query.boundValues();
return false;
}
}
This, outputs:
bool EditableSqlModel::setFirstName(int, int, const QString&) "UPDATE :table SET desc = :string WHERE id = :id;"
bool EditableSqlModel::setFirstName(int, int, const QString&) QSqlError(-1, "Parameter count mismatch", "")
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") ) )
But, if I use this instead, it just works!
bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
{
QSqlQuery query;
//query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
QString table_str;
switch(table) {
case 1:
table_str = "presupuestados_cascos_y_accesorios";
break;
case 2:
table_str = "presupuestados_complementos";
break;
case 3:
table_str = "presupuestados_puertas_y_cajones";
break;
case 4:
table_str = "presupuestados_electrodomesticos";
break;
default:
return false;
}
/* query.bindValue(":table", table_str);
query.bindValue(":string", firstName);
query.bindValue(":id", id);
*/
if(query.exec(QString("update %1 set desc = '%2' where id = %3")
.arg(table_str)
.arg(firstName)
.arg(id)))
{
return true;
}
else
{
qDebug() << Q_FUNC_INFO << query.lastQuery();
qDebug() << Q_FUNC_INFO << query.lastError();
qDebug() << Q_FUNC_INFO << query.boundValues();
return false;
}
}
Noting that:
The argument number, according to the debug output, is exactly the same than the number of placeholders
The type of such arguments is the same in both cases
The table is the same, and the database is always the default one
Can someone enlighten me on why this is failing?
Thank you.
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.
bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
{
QSqlQuery query;
query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
QString table_str;
switch(table) {
case 1:
table_str = "presupuestados_cascos_y_accesorios";
break;
case 2:
table_str = "presupuestados_complementos";
break;
case 3:
table_str = "presupuestados_puertas_y_cajones";
break;
case 4:
table_str = "presupuestados_electrodomesticos";
break;
default:
return false;
}
query.bindValue(":table", table_str);
query.bindValue(":string", firstName);
query.bindValue(":id", id);
if(query.exec())
{
return true;
}
else
{
qDebug() << Q_FUNC_INFO << query.lastQuery();
qDebug() << Q_FUNC_INFO << query.lastError();
qDebug() << Q_FUNC_INFO << query.boundValues();
return false;
}
}
This, outputs:
bool EditableSqlModel::setFirstName(int, int, const QString&) "UPDATE :table SET desc = :string WHERE id = :id;"
bool EditableSqlModel::setFirstName(int, int, const QString&) QSqlError(-1, "Parameter count mismatch", "")
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") ) )
But, if I use this instead, it just works!
bool EditableSqlModel::setFirstName(int table, int id, const QString &firstName)
{
QSqlQuery query;
//query.prepare("UPDATE :table SET desc = :string WHERE id = :id;");
QString table_str;
switch(table) {
case 1:
table_str = "presupuestados_cascos_y_accesorios";
break;
case 2:
table_str = "presupuestados_complementos";
break;
case 3:
table_str = "presupuestados_puertas_y_cajones";
break;
case 4:
table_str = "presupuestados_electrodomesticos";
break;
default:
return false;
}
/* query.bindValue(":table", table_str);
query.bindValue(":string", firstName);
query.bindValue(":id", id);
*/
if(query.exec(QString("update %1 set desc = '%2' where id = %3")
.arg(table_str)
.arg(firstName)
.arg(id)))
{
return true;
}
else
{
qDebug() << Q_FUNC_INFO << query.lastQuery();
qDebug() << Q_FUNC_INFO << query.lastError();
qDebug() << Q_FUNC_INFO << query.boundValues();
return false;
}
}
Noting that:
The argument number, according to the debug output, is exactly the same than the number of placeholders
The type of such arguments is the same in both cases
The table is the same, and the database is always the default one
Can someone enlighten me on why this is failing?
Thank you.