I have the same problem, bellow my code if some one can help me please.
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
def run_script(iface):
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "Base_test", "user", "****")
uri.setDataSource("public", "mytable", "geom","")
vlayer = QgsVectorLayer(uri.uri(), "mytable", "user")
QgsMapLayerRegistry.instance().addMapLayers([layer])
When I run this code I haven't un error but i see nothing on my QGIS interface.
I'm using, QGIS 3.12.
from qgis.core import *
from qgis.core import QgsProject
from PyQt5.QtCore import QFileInfo
from qgis.core import QgsVectorLayer, QgsDataSourceUri
from qgis.utils import *
import processing
def run_script(iface):
uri = QgsDataSourceUri()
uri.setConnection("localhost", "5432", "Base_test", "Nassim", "*****")
uri.setDataSource("public", "BPE", "geom")
layer = QgsVectorLayer(uri.uri(), "BPE", "Nassim")
QgsProject.instance().addMapLayer(layer)
This is my code whith the solution given by @Vincent Bré, but it still doesn't work
2 Answers 2
You should use QgsProject
instead of QgsMapLayerRegistry
.
To add a single layer, you can use the addMapLayer
method.
You can use the following code:
uriPG = QgsDataSourceUri()
uriPG.setConnection("localhost", "5432", "database", "user", "password")
uriPG.setDataSource("schema", "name_table", "geometry_column")
layer = QgsVectorLayer(uriPG.uri(), "name_table_in_qgis", "user")
QgsProject.instance().addMapLayer(layer)
-
Thank you for your answser, it stil not working for me. I don't know why besacuse it's excatlty you code what I put whith the good parameters .in addition, it makes my QGIS buggy and you have to relaunch itnmokht97– nmokht972020年06月19日 09:01:55 +00:00Commented Jun 19, 2020 at 9:01
-
Do you have an error message?Vincent Bré– Vincent Bré2020年06月19日 09:24:03 +00:00Commented Jun 19, 2020 at 9:24
-
-
My code works. You run it from the Python console in QGIS? Make sure you check the name of your table, the geometry columnVincent Bré– Vincent Bré2020年06月19日 09:28:23 +00:00Commented Jun 19, 2020 at 9:28
-
>>>exec(open('C:/Users/NASSIM~1.MOK/AppData/Local/Temp/tmpmv2m58vg.py'.encode('utf-8')).read())nmokht97– nmokht972020年06月19日 09:44:13 +00:00Commented Jun 19, 2020 at 9:44
I made a modification on layer = QgsVectorLayer(uri.uri(), "BPE", "") removing the user in this function, what stope the bug, I'm sure of my parameters because when I load the same table frome postgis using the interface all is ok. but whene I'm using Pyqgis it give me "unusing layer"
uriPG = QgsDataSourceUri()
uriPG.setConnection("localhost", "5432", "Base_test", "Nassim", "******")
uriPG.setDataSource("public", "CABLES", "geom")
layer = QgsVectorLayer(uriPG.uri(), "BPE",)
if not layer.isValid():
print("Layer %s did not load" %layer.name())
QgsProject.instance().addMapLayer(layer)
bellow the picture of my result in the console and QGIS interface
it gives " an using layer"
-
I fixed my problem, I juste changed in 'code'layer = QgsVectorLayer(uriPG.uri(), "BPE","postgres") , I don't use my own user here.nmokht97– nmokht972020年06月23日 07:25:21 +00:00Commented Jun 23, 2020 at 7:25
run_script(iface)
? It's because all the code you show us is only declaring the function but you need to call it to work