Check if QSqlQuery::addBindValue() has failed

426 views Asked by At

How can I check if QSqlQuery::addBindValue() has failed? For example, there might not be any more placeholders left to be bound. Consider this code:

QSqlQuery q;
q.prepare("INSERT INTO table1 (field1) VALUES (?)");
q.addBindValue("value1");
q.addBindValue("value2"); // this one should fail

However, addBindValue() is a void, so it doesn't return information about whether it succeeded or failed.

1

There are 1 answers

0
sashoalm On

I finally stepped into the sources, and it seems you really can't find out if it has failed. It doesn't set the last error or any other error code:

void QSqlResult::bindValue(int index, const QVariant& val, QSql::ParamType paramType)
{
    Q_D(QSqlResult);
    d->binds = PositionalBinding;
    QList<int>& indexes = d->indexes[d->fieldSerial(index)];
    if (!indexes.contains(index))
        indexes.append(index);
    if (d->values.count() <= index)
        d->values.resize(index + 1);
    d->values[index] = val;
    if (paramType != QSql::In || !d->types.isEmpty())
        d->types[index] = paramType;
}