我可以在PostGIS表中运行SQL来加载QGIS3.16 (运行UbuntuDesktop20.04)中的查询,如下所示:
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "dbname", "username", "password")
print("Connection Successful")
nb = 1050130
fields = '*'
sql ='''(SELECT {} FROM montebelodosul.cadastro_urbano_montebelodosul_p WHERE numero_cadastro = {})'''.format(fields,nb)
# Retrieve the query table
uri.setDataSource('', f'({sql})', 'geom', '', 'id')
# add the layer to the canvas
pg_layer = QgsVectorLayer(uri.uri(False), "queryLayer", "postgres")
QgsProject.instance().addMapLayer(pg_layer)我不使用Psycopg2。在运行SELECT之前,有人会给我提供一个洞察力,或者在运行SELECT之前如何使用PyQGIS在表上运行更新或插入的方向吗?
发布于 2022-02-17 08:35:29
我认为您应该先加载该层,以便更新、插入、删除其功能。请参阅这里中的文档。
https://stackoverflow.com/questions/71058739
复制相似问题