我可以在 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 的方向吗?