While running the following code:
from qgis.utils import iface
from qgis.core import QgsVectorFileWriter
canvas = iface.mapCanvas()
layer = canvas.currentLayer()
print(layer.name())
options = QgsVectorFileWriter.SaveVectorOptions()
options.actionOnExistingFile = QgsVectorFileWriter.CreateOrOverwriteLayer
#options.layerName = "_".join(layer.name().split(' '))
writer = QgsVectorFileWriter.writeAsVectorFormat(layer, "C:/Users/ilyasse2.0/Documents/SQRLAND/hh.gpkg", options)
print(writer)
I got this error:
(2, 'Opening of data source in update mode failed (OGR error: )')
The layer is created by with:
from qgis.core import QgsVectorLayer
layer = QgsVectorLayer("Point", 'layerName', "memory")
I tried to use this solution: Adding layers to GeoPackage using PyQGIS but it is not working for me.
native:savefeatures
. See this answerQgsVectorFileWriter.writeAsVectorFormat()
also works for a single layer, but you have Python to iterate.