2

I am trying to convert a shapefile to SQLite file in QGIS 3.26 using Python.

Here is my code:

def convert_to_sqlite(self):
 input_shp = QgsVectorLayer("C:\shapeFile\BMR_Zones_Corrected_519.shp","polygon","ogr")
 print(input_shp.isValid())
 QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.sqlite","UTF-8",input_shp.crs(),"SQLite")

This is the error I am facing:

warning:C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py:477: DeprecationWarning: QgsVectorFileWriter.writeAsVectorFormat() is deprecated
QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.gpkg","UTF-8",input_shp.crs(),"GPKG")
 
traceback: File "C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py", line 602, in run
result = self.dlg.exec_()
File "C:\Users/MTTUSER/AppData/Roaming/QGIS/QGIS3\profiles\default/python/plugins\Network_Processing\ABTDModel.py", line 477, in convert_to_sqlite
QgsVectorFileWriter.writeAsVectorFormat(input_shp,"C:\shapeFile\BMR_Zones_Corrected_519.gpkg","UTF-8",input_shp.crs(),"GPKG")
 
nmtoken
13.6k5 gold badges39 silver badges91 bronze badges
asked Nov 9, 2022 at 10:59
1

1 Answer 1

2

Try this:

shape = r'/home/bera/GIS/Data/testdata/subway.shp'
output = r'/home/bera/GIS/Data/testdata/subway.sqlite'
vl = QgsVectorLayer(shape, 'temp')
options = QgsVectorFileWriter.SaveVectorOptions()
options.driverName = "SQLite"
QgsVectorFileWriter.writeAsVectorFormatV2(vl, output, QgsCoordinateTransformContext(), options)
answered Nov 10, 2022 at 18:13
1
  • 1
    Thank you, this worked for me. Commented Dec 1, 2022 at 6:54

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.