diff --git a/src/sql/kernel/qsqlquery.cpp b/src/sql/kernel/qsqlquery.cpp index eff211cc37..07b23f7f03 100644 --- a/src/sql/kernel/qsqlquery.cpp +++ b/src/sql/kernel/qsqlquery.cpp @@ -405,6 +405,24 @@ QVariant QSqlQuery::value(int index) const return QVariant(); } +/*! + \overload + + Returns the value of the field called \a name in the current record. + If field \a name does not exist an invalid variant is returned. + + This overload is less efficient than \l{QSqlQuery::}{value()} +*/ + +QVariant QSqlQuery::value(const QString& name) const +{ + int index = d->sqlResult->record().indexOf(name); + if (index > -1) + return value(index); + qWarning("QSqlQuery::value: unknown field name '%s'", qPrintable(name)); + return QVariant(); +} + /*! Returns the current internal position of the query. The first record is at position zero. If the position is invalid, the diff --git a/src/sql/kernel/qsqlquery.h b/src/sql/kernel/qsqlquery.h index 9b4f35c775..9d660e08c1 100644 --- a/src/sql/kernel/qsqlquery.h +++ b/src/sql/kernel/qsqlquery.h @@ -86,6 +86,7 @@ public: void setForwardOnly(bool forward); bool exec(const QString& query); QVariant value(int i) const; + QVariant value(const QString& name) const; void setNumericalPrecisionPolicy(QSql::NumericalPrecisionPolicy precisionPolicy); QSql::NumericalPrecisionPolicy numericalPrecisionPolicy() const; diff --git a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp index 2030e2c4bf..26768f21ed 100644 --- a/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp +++ b/tests/auto/sql/kernel/qsqlquery/tst_qsqlquery.cpp @@ -903,6 +903,7 @@ void tst_QSqlQuery::value() while ( q.next() ) { QCOMPARE( q.value( 0 ).toInt(), i ); + QCOMPARE( q.value( "id" ).toInt(), i ); if ( db.driverName().startsWith( "QIBASE" ) ) QVERIFY( q.value( 1 ).toString().startsWith( "VarChar" + QString::number( i ) ) );