PDA

View Full Version : placeholder problem



juliano.gomes
27th November 2015, 20:06
Hello,
I'm with problem in my UPDATE clause using placeholder (https://www.sqlite.org/lang_expr.html#varparam) (sqlite3). In some fields the placeholder return a incorrect data, even i verifying that the data that i send to UPDATE is correct.

Eg:the output from "query2.value(0).toInt()" is 2 (a integer value, like i expected), but the placeholder ":id_city" stores the following value: 1_city



QSqlDatabase con = QSqlDatabase::addDatabase("QSQLITE");
con.setDatabaseName(_DB);
con.open();

QSqlQuery query;
// the problem occur ins some cases with different data types, like integer directly (0), query2 return and text from qlineedit, as with diferent object types (qlineedit, qcombobox, qcheckbox)
query.prepare("UPDATE table SET id_country=:id_country, id_city=:id_city, name=:name ...... WHERE id=:id"
query.bindValue(":id_country",1);
query.bindValue(":id_city",query2.value(0).toInt());
query.bindValue(":name",ui->QLineedit->text());
.
.
.
query.bindValue(":id",_ID);
query.exec();


someone known where is the problem? could help me please?

Thanks!
Juliano

anda_skoa
28th November 2015, 09:43
Since "1_city" looks like the ":id" part got replaced, have you tried using placeholders without _ ?

Cheers,
_

juliano.gomes
30th November 2015, 18:36
Hello,

Thanks anda_skoa, after several testing I learned:

1. dont use _ in placeholders;
2. dont use number in begin of a table field (my real table has this structure. eg: 1_city (to remember me that this field belongs to group 1));
3. if the error "parameter count mismatch" is showed when building de code, check the SQL statement, probably it have some problem.

A good way I've found to check my SQL statement with placeholders:


QString getLastExecutedQuery(const QSqlQuery& query)
{
QString str = query.lastQuery();
QMapIterator<QString, QVariant> it(query.boundValues());
while (it.hasNext())
{
it.next();
str.replace(it.key(),it.value().toString());
}
return str;

// call it after your query.exec(), like "cout << getLastExecutedQuery(query).toStdString();"
}


solved

hugs!
Juliano