0

我可以在 PostGIS 表中运行 SQL 以在 QGIS3.16(运行 Ubuntu Desktop 20.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 之前,有人会给我一个见解或指出如何使用 PyQGIS 在表上运行 UPDATE 或 INSERT 的方向吗?

4

1 回答 1

0

我认为您应该首先加载图层以更新、插入、删除其功能。请参阅此处的文档。

于 2022-02-17T08:35:29.727 回答