QSqlQuery and SQL text field escape sequences
-
Hi all,
I need to create "partial" SQL requests which only contains the "value" part.
I some of those values there are text fields.
I known, QSqlQuery can handle this very nicely using prepare() and bindValue().
But is there a way to get the generated SQL query?I just need the SQL query string, not to execute the query.
How can I achieve this?Best regards
-
@VRonin Thank's a lot! You save my day.
In fact, it is easy, just setup a QSqlField, get the DB driver from QSqlDataBase, and call formatValue().
QSqlDataBase db = QSqlDatabase::addDatabase(...) ... QSqlField f(QLatin1String(""), QVariant::String); f.setValue("The string to be converted"); QString formatedValue = db->driver()->formatValue(f);
-
@KroMignon lmgtfy:
-
from qsqlresult.cpp
QString query = lastQuery(); if (d->binds == NamedBinding) { int i; QVariant val; QString holder; for (i = d->holders.count() - 1; i >= 0; --i) { holder = d->holders.at(i).holderName; val = d->values.value(d->indexes.value(holder).value(0,-1)); QSqlField f(QLatin1String(""), val.type()); f.setValue(val); query = query.replace(d->holders.at(i).holderPos, holder.length(), driver()->formatValue(f)); } } else { QString val; int i = 0; int idx = 0; for (idx = 0; idx < d->values.count(); ++idx) { i = query.indexOf(QLatin1Char('?'), i); if (i == -1) continue; QVariant var = d->values.value(idx); QSqlField f(QLatin1String(""), var.type()); if (var.isNull()) f.clear(); else f.setValue(var); val = driver()->formatValue(f); query = query.replace(i, 1, driver()->formatValue(f)); i += val.length(); } }
It's not trivial but you can probably achieve it without accessing the private API either
-
@VRonin Thank's a lot! You save my day.
In fact, it is easy, just setup a QSqlField, get the DB driver from QSqlDataBase, and call formatValue().
QSqlDataBase db = QSqlDatabase::addDatabase(...) ... QSqlField f(QLatin1String(""), QVariant::String); f.setValue("The string to be converted"); QString formatedValue = db->driver()->formatValue(f);