[QGIS-Developer] Create plain table (without geometry) in gpkg?

Julien Cabieces julien.cabieces at oslandia.com
Wed Jun 24 05:57:03 PDT 2020


Hi alls,

I was looking to create an empty GPKG layer and try the solution Even provide, but
it creates an invalid file.

fields=QgsFields()
fields.append(QgsField("note", QVariant.Double))
QgsVectorFileWriter.create("/tmp/test.gpkg", fields, QgsWkbTypes.MultiLineString, QgsProject.instance().crs(), QgsCoordinateTransformContext(), QgsVectorFileWriter.SaveVectorOptions())

I try also:
lyr = "mylayer"
QgsVectorFileWriter.create("/tmp/again.gpkg", fields, QgsWkbTypes.MultiLineString, QgsProject.instance().crs(), QgsCoordinateTransformContext(), QgsVectorFileWriter.SaveVectorOptions(), QgsFeatureSink.SinkFlags(), None, lyr)

It looks like there is no layer in the produced GPKG. Is it an issue, or am I misunderstanding the use of QgsVectorFileWriter?

Kind regards,
Julien


> On mardi 23 juin 2020 15:34:58 CEST Raymond Nijssen wrote:
>> My python plugin should create a table without geometry in a gpkg to
>> store some additional (meta) info about the layers. I could use python
>> bindings for sqlite of course, but the plugin users might not have that
>> installed.
>> 
>> What are the options from within qgis? Is python-sqlite available, or
>> can I run sql queries in antother way? Or can I create a table similar
>> to creating a new maplayer?
>> 
>> My (ugly) solution would be creating a point layer and use that
>> attribute table and storing POINT(0 0) as geometries.
>> Or even worse, creating a .csv sidecar file for my gpkg.
>> 
>> Hope anyone here knows a better solution!
>
> Raymond,
>
> You should be able to just use the normal QGIS vector API for that. For example 
> QgsVectorFileWriter.create() with geometryType = NoGeometry
>
> Even



More information about the QGIS-Developer mailing list