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

Nathan Woodrow madmanwoo at gmail.com
Fri Aug 30 02:48:13 PDT 2013


Have you tried just setting it to None?

- Nathan


On Fri, Aug 30, 2013 at 7:38 PM, Alexandre Neto <senhor.neto at gmail.com>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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.osgeo.org/pipermail/qgis-developer/attachments/20130830/260d6581/attachment.html>


More information about the Qgis-developer mailing list