I am relatively new to PyQGIS and am attempting to save a SpatialLite layer into a new layer (preferably in the same database) with a different CRS (I had it working in shapefile but have had to move the data to SpatialLite which I have not used before). I keep on getting errors about the types of my variables, but am unable to figure out how else to do it and I can't find an example online which says what each of the variables are when exporting to SQLite.

layers = QgsProject.instance().mapLayersByName('SiteCard_AMG')
layer = QgsVectorLayer(layers[0].dataProvider().dataSourceUri(), '', 'ogr')


error = QgsVectorFileWriter.writeAsVectorFormat(layer, 'C:\\Users\\Megan\\Documents\\new.sqlite', 'utf-8', crs, 'SQLite', False, None, ['SPATIALITE=YES'])

error as follows;

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.10\apps\Python37\lib\code.py", line 90, in runcode
    exec(code, self.locals)
  File "<input>", line 1, in <module>
  File "<string>", line 47, in <module>
TypeError: QgsVectorFileWriter.writeAsVectorFormat(): arguments did not match any overloaded call:
  overload 1: argument 7 has unexpected type 'NoneType'
  overload 2: argument 4 has unexpected type 'QgsCoordinateReferenceSystem'
  overload 3: argument 3 has unexpected type 'str'

1 Answer 1


the following code works in my case to export a vector layer to an sqlite database:

layer = iface.activeLayer()
crs = QgsCoordinateReferenceSystem("epsg:4326")
s_out_file = r"%PATH_TO_OUTPUT_FILE.sqlite"
err = QgsVectorFileWriter.writeAsVectorFormat(layer, s_out_file, "utf-8", crs, driverName='SQLite', onlySelected=False, datasourceOptions=['SPATIALITE=YES'])

The call to writeAsVectorFormat() is very similar to yours, but without the None parameter (7th parameter in your case), which is in fact not needed.

2 side notes based on your example code:

  1. Could you not use directly layers[0] as the vector layer to be saved in the output sqlite database (i.e., without instantiating the layer variable)?
  2. writeAsVectorFormat was deprecated in QGIS 3.10 in favor of writeAsVectorFormatV2. So, depending on the version you are using, you might want to adapt the code. A possible code snippet that uses writeAsVectorFormatV2 and changes the CRS of the output layer is the following:
layer = iface.activeLayer()
s_out_file = r"%PATH_TO_OUTPUT_FILE_V2.sqlite"
ctc = QgsProject.instance().transformContext()
o_save_options = QgsVectorFileWriter.SaveVectorOptions()
o_save_options.layerName = layer.name()
o_save_options.driverName = 'SQLite'
o_save_options.fileEncoding = 'utf-8'
o_save_options.onlySelectedFeatures = False
o_save_options.layerOptions = ['SPATIALITE=YES']
o_save_options.ct = QgsCoordinateTransform(QgsCoordinateReferenceSystem("EPSG:2056"), QgsCoordinateReferenceSystem("EPSG:4326"), QgsProject.instance())
err = QgsVectorFileWriter.writeAsVectorFormatV2(layer, s_out_file, ctc, o_save_options)

I hope this helps.

  • thanks for the quick answer.
    – mallen
    Jun 25, 2020 at 2:13
  • my issue with using writeAsVectorFormatV2 is I haven't been able to successfully set a new crs. If you could please post me an example I would like to shift to use V2, thanks.
    – mallen
    Jun 25, 2020 at 2:15
  • Hi @mallen, I have edited my original answer to provide an example using writeAsVectorFormatV2. I hope it will work. Cheers.
    – fastest
    Jun 25, 2020 at 17:02
  • It worked, thank you
    – mallen
    Jun 26, 2020 at 0:42
  • Hi @mallen, thanks for letting me know. Can you then accept the answer as the correct one? Thank you.
    – fastest
    Jun 26, 2020 at 10:03

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.