PDA

View Full Version : QSqlQuery and prepare/bindValue problem!



QNewbie
15th September 2014, 21:50
Hey all, I'm having a problem, using this:

QSqlQuery peticion(enlace);
QString SQL("UPDATE mercaderias SET :1 = :4 WHERE Nombre=:3 AND Proveedor=:4");
QString columna;
switch(uno.column()) {
case 0:
columna="Nombre";
break;
case 1:
columna="Proveedor";
break;
case 2:
columna="Cantidad";
break;
case 3:
columna="Precio";
break;
}
peticion.prepare(SQL);
peticion.bindValue(":1",columna);
peticion.bindValue(":2",uno.data().toString());
peticion.bindValue(":3",ui->Tabla->model()->index(uno.row(),0).data().toString());
peticion.bindValue(":4",ui->Tabla->model()->index(uno.row(),2).data().toString());

if ( !peticion.exec() ) {
qDebug() << "Error al actualizar";
qDebug() << peticion.executedQuery();
qDebug() << peticion.lastError().text();
}

peticion.executedQuery() returns the values inside quotes (like 'value') instead of them being raw. What can I do to fix this?

d_stranz
15th September 2014, 22:44
Don't convert them to strings with QVariant::toString(). All you are doing is converting the original QVariant returned by the data() method (with whatever type it contains) to a QString and then back to a QVariant with a string type.

QNewbie
15th September 2014, 22:52
Line 19, using the QString directly, does not work not even if converted to a c_str()
Further ideas? Since the information is good, but does not solve the actual problem.

d_stranz
16th September 2014, 05:13
Line 19, using the QString directly, does not work not even if converted to a c_str()

And "does not work" means what, exactly? Won't compile? Creates malformed SQL? Doesn't return results when the query is executed?

Sort of hard to know what's going on when we aren't looking over your shoulder at your PC and you give vague replies.

There is no QString::c_str() method, so I don't know what you mean by this.