1

I have a Mircosoft SQL Server database. In a table I need to save two different geometries in a same record (row). Is for this I create two columns. The second one I created adding a column using MS SQL Management Studio (I only add column and I don't do anything more).

When I try to save data in the second column, nothing save in the column (geom_2). I use the same gramatical order to save data in column one (geom).

This works: (column 1)

uri = QgsDataSourceUri()
uri.setConnection(server,"", DDBB, user, password)
uri.setDataSource("dbo",table,'geom')
layer=QgsVectorLayer(uri.uri(), "Open "+table, "mssql") 
layer.selectByExpression(scope,QgsVectorLayer.SetSelection)
fid = layer.selectedFeatureIds()[0]
values=QgsGeometry() # for simplify I don't put geometry value
layer.dataProvider().changeGeometryValues({ fid : values})
layer.updateExtents()

This doesn't work:

uri = QgsDataSourceUri()
uri.setConnection(server,"", DDBB, user, password)
uri.setDataSource("dbo",table,'geom_2')
layer=QgsVectorLayer(uri.uri(), "Open "+table, "mssql") 
layer.selectByExpression(scope,QgsVectorLayer.SetSelection)
fid = layer.selectedFeatureIds()[0]
values=QgsGeometry() # for simplify I don't put geometry value
layer.dataProvider().changeGeometryValues({ fid : values})
layer.updateExtents()

By the moment I resolve with and SQL order:

UPDATE table SET geom_2 = (geometry::STGeomFromText('{}', 0)) WHERE Id=1""".format(geom.asWkt())

After that I will need to show this two geometry in QGIS canvas.

How to display a table with more than one geometry column in QGIS?

0

Your Answer

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

Browse other questions tagged or ask your own question.