From 49b23127caaa8854f8fb05995d0119d4538dabed Mon Sep 17 00:00:00 2001 From: rldhont Date: Thu, 16 Nov 2017 14:29:18 +0100 Subject: [PATCH] [BUGFIX][Spatialite] Setting default value in loadFields with the right index Because Geomtry field is not added to fields list, the index do not respect the index in the table. It's necessary to have a realFieldIndex. --- .../spatialite/qgsspatialiteprovider.cpp | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-) diff --git a/src/providers/spatialite/qgsspatialiteprovider.cpp b/src/providers/spatialite/qgsspatialiteprovider.cpp index cc9653305ed..78332b45e32 100644 --- a/src/providers/spatialite/qgsspatialiteprovider.cpp +++ b/src/providers/spatialite/qgsspatialiteprovider.cpp @@ -949,9 +949,13 @@ void QgsSpatiaLiteProvider::loadFields() ; else { + int realFieldIndex = 0; for ( i = 1; i <= rows; i++ ) { QString name = QString::fromUtf8( results[( i * columns ) + 1] ); + if ( name.toLower() == mGeometryColumn ) + continue; + QString type = QString::fromUtf8( results[( i * columns ) + 2] ).toLower(); QString pk = results[( i * columns ) + 5]; if ( pk.toInt() != 0 ) @@ -962,17 +966,15 @@ void QgsSpatiaLiteProvider::loadFields() pkName = name; else pkName.clear(); - mPrimaryKeyAttrs << i - 1; + mPrimaryKeyAttrs << realFieldIndex; QgsDebugMsg( "found primaryKey " + name ); } - if ( name.toLower() != mGeometryColumn ) - { - const TypeSubType fieldType = getVariantType( type ); - mAttributeFields.append( QgsField( name, fieldType.first, type, 0, 0, QString(), fieldType.second ) ); - } + const TypeSubType fieldType = getVariantType( type ); + mAttributeFields.append( QgsField( name, fieldType.first, type, 0, 0, QString(), fieldType.second ) ); - insertDefaultValue( i - 1, QString::fromUtf8( results[( i * columns ) + 4] ) ); + insertDefaultValue( realFieldIndex, QString::fromUtf8( results[( i * columns ) + 4] ) ); + realFieldIndex += 1; } } sqlite3_free_table( results );