PyQGIS: How to add temporary PostGIS table to canvas?

المشرف العام

Administrator
طاقم الإدارة
I'm working with PostGIS database. I would like to create temporary table with geometry and add it as a layer to QGIS. There is no problem with regular tables, but I can't figure out how to add temporary tables. I checked source code for DB Manager plugin and found this key line: uri.setDataSource("", u"(%s\n)" % sql, geomCol, "", uniqueCol). With this in mind I created my code:

uri = QgsDataSourceURI()uri.setConnection(server, port, db_name, login, password)sql = "CREATE TEMPORARY TABLE temp_1 AS SELECT * FROM some_spatial_table"uri.setDataSource('', u'(%s\n)' % sql, 'geom', '', 'gid') # 'geom' and 'gid' are present in the tablevlayer = QgsVectorLayer(uri.uri(), 'layer_name', "postgres")QgsMapLayerRegistry.instance().addMapLayer(vlayer)The SQL-query is Ok - I'm able to execute it in DB Manager and add resulting table to map. But my code doesn't produce a layer in QGIS and there is no exception raised. What is wrong?



أكثر...
 
أعلى