[Qgis-developer] How to use QPyNullVariant() to "empty" primary key fields in spatialite layer

Matthias Kuhn matthias.kuhn at gmx.ch
Fri Aug 30 02:48:00 PDT 2013


Hi Alexander,

You should be able to use either of the following:

attributes[j] = None
attributes[j] = NULL # first do: from qgis.core import NULL
attributes[j] = QPyNullVariant(int)

If this is not working, it should be considered a bug in the spatialite 
provider. I realized, there's used a lot of QVariant::Invalid and 
QVariant() instead of NULL QVariants initialized with QVariant( 
TypeName ) in the spatialite provider,  so I would not be surprised.

Cheers
Matthias

On Fre 30 Aug 2013 11:38:54 CEST, Alexandre Neto wrote:
> In the multipartsplit plugin [1] I'm trying to solve the primary key
> (pk) fields problem in postgis and spatialite like in it was done in
> the splitFeatures() [2]
>
> I have no problems with Postgis provider, but in spatialite setting pk
> fields to NULL does not work as I expected, and spatialite does not
> choose a new non used id by it self. (Making it impossible to commit
> the changes without manually introduce some id)
>
> In splitFeatures() [2] when a field is in provider.pkAttributeIndexes
> <http://www.qgis.org/api/classQgsVectorDataProvider.html#a6e19d018aa0b88da21fdb9d02c5e8a60>(),
> but has no default value, the filed is set to empty with this
>
> newAttributes[ pkIdx ] = QVariant();
>
> But i'm not being able to use it in my code, since it's says
> QVariant() can not be instantiated. Actually this is said in here [3].
> And it says that Null QVariant() are now converted in QPyNullVariant
>
> Can I use QPyNullVariant to keep the pk field empty, so that
> spatialite automatically choose a adequate int value? How?
>
> I tried the code bellow but it did not work.
>
>     # Get attributes from original feature
>     new_attributes = feature.attributes()
>
>
>
>     # When attribute is a Primary Key, replace by provider default value
>
>
>
>     for j in provider.pkAttributeIndexes():
>         if provider.defaultValue(j):
>             new_attributes[j] = provider.defaultValue(j)
>         else:
>             new_attributes[j] = QPyNullVariant('int')
>
>
>
> Thanks.
>
> Alexandre Neto
>
> [1] http://plugins.qgis.org/plugins/splitmultipart/
> [2] http://www.qgis.org/api/qgsvectorlayereditutils_8cpp_source.html#l00247
> [3] http://hub.qgis.org/wiki/quantum-gis/Python_plugin_API_changes_from_18_to_20
>
>
>
>
> _______________________________________________
> Qgis-developer mailing list
> Qgis-developer at lists.osgeo.org
> http://lists.osgeo.org/mailman/listinfo/qgis-developer




More information about the Qgis-developer mailing list