Conexão com banco utilizando o Pyqgis

12 views
Skip to first unread message

Luciana Estevam

unread,
Dec 10, 2020, 10:53:16 AM12/10/20
to postgis-brasil
Alguém poderia me ajudar, estou tentando fazer conexão com banco de dados usando o pyqgis e esta dando erro nas duas últimas linhas 

-- from PyQt5.QtCore import *
from PyQt5.QtGui import *
from qgis.core import *
from qgis.gui import *

def run_script ( iface ):
    uri = QgsDataSourceUri ()
    uri.setConnection ("host", "5432", "leitura", "senha")
    uri.setDataSource ("geo", "area_autorizada", "geom", "cod_projeto = 63")
    layer = QgsVectorLayer (uri.uri (), "reefs" , "postgres")
    if not layer.isValid ():
    print "layer%s did not load"% Layer.name ()
    QgsProject.instance().addMapLayer([layer]) ----


Erro:

Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 7
    print "layer%s did not load"% Layer.name ()
                                              ^
IndentationError: expected an indented block
    QgsProject.instance().addMapLayer([layer])
Traceback (most recent call last):
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\code.py", line 63, in runsource
    code = self.compile(source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 168, in __call__
    return _maybe_compile(self.compiler, source, filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 99, in _maybe_compile
    raise err1
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 87, in _maybe_compile
    code1 = compiler(source + "\n", filename, symbol)
  File "C:\PROGRA~1\QGIS3~1.8\apps\Python37\lib\codeop.py", line 133, in __call__
    codeob = compile(source, filename, symbol, self.flags, 1)
  File "<input>", line 1
    QgsProject.instance().addMapLayer([layer])
    ^
IndentationError: unexpected indent
Reply all
Reply to author
Forward
0 new messages