Answer the question
In order to leave comments, you need to log in
Why are values not substituted through bindValue in QSqlQuery?
Hello! I'm trying to add data to the database via bindValue, however, the data is not being bound.
I do this:
if(query.prepare("INSERT INTO messages (date, time, username, phone_number, text_message) VALUES (:date, :time, :name, :phone, :text)"))
{
query.bindValue(":date","1");
query.bindValue(":time","1");
query.bindValue(":name","1");
query.bindValue(":phone","1");
query.bindValue(":text","1");
qDebug()<<query.lastError();
query.exec();
}
qDebug()<<query.executedQuery();
"INSERT INTO messages (date, time, username, phone_number, text_message) VALUES (?, ?, ?, ?, ?)"
qDebug()<<query.boundValues();
QMap((":date", QVariant(QString, "1"))(":name", QVariant(QString, "1"))(":phone", QVariant(QString, "1"))(": text", QVariant(QString, "1"))(":time", QVariant(QString, "1")))
Answer the question
In order to leave comments, you need to log in
query.executedQuery() won't show the real query, try this:
QString lastQuery = query.executedQuery();
for (int i = 0, j = 0; j < query.boundValues().size(); ++j)
{
i = lastQuery.indexOf(QLatin1Char('?'), i);
if (i <= 0) break;
const QVariant& var = query.boundValue(j);
QSqlField field(QString(""), var.type());
if (var.isNull()) field.clear();
else field.setValue(var);
QString formatV = query.driver()->formatValue(field);
lastQuery.replace(i, 1, formatV);
i += formatV.length();
}
qDebug()<<"SQL Query error:"<<lastQuery;
bool ok = query.prepare("INSERT INTO messages (date, time, username, phone_number, text_message) VALUES (:date, :time, :name, :phone, :text)");
query.bindValue(":date","1");
query.bindValue(":time","1");
query.bindValue(":name","1");
query.bindValue(":phone","1");
query.bindValue(":text","1");
if (ok)
query.exec();
else
qDebug()<<query.lastError();
Make an else branch after prepare and display query.lastError() via qDebug() and see what it says to you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question