This is a follow up to this question: Export vector layer to sqlite database using pyqgs

I am trying to import a QGIS polygon layer into a spatialite db. I was able to add a geometry column to my table using

cur.execute("SELECT InitSpatialMetadata()")     
cur.execute("SELECT AddGeometryColumn('Polygonx', 'geometry', 4326, 'POLYGON', 'XY')")

Subsequently, I am using the following line to insert my values, inlcuding the geometry

cur.executemany("INSERT INTO Polygonx VALUES (?,?,?,?)", feature_list)


feature_list = []    
for feature in vlyr.getFeatures():
    attributes = feature.attributes()
    new_feature = (feature.id(), attributes[0], attributes[1], feature.geometry().exportToWkt())

When using the db manager to check the table, however, the value of the geometry column is always NULL. Any idea what wrong with my code?


(Only a partial answer)

The geometry column in spatialite is not WKT. I think you would need to convert the WKT representiation to a "Geometry BLOB". So either ST_GeomFromText() in your INSERT statement, or the python equivalent.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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