TransWikia.com

PyQGIS: how to stop script processing without shutting down QGIS

Geographic Information Systems Asked by wanderzen on January 20, 2021

I’m looking for a way to aborpt a script execution if a sql query returns some results. I’ve found a few solutions on the forum but all of them lead to shutting down QGIS (thing I precisely want to avoid).

This question has already been discussed here and the conclusion was that it was not possible yet.

7 years later, I’m wondering if this has eventually been made possible ?

my script:

conn = psycopg2.connect(db_key)

cur = conn.cursor()

try:
    cur.execute("""
-- 
SELECT * FROM a.table1 WHERE ps_code like 'test_erreur'
""")

except:
    print ("Can't run this SQL query")

rows = cur.fetchall()

if rows is None: 
    print("No error found")
if len(rows) > 0:
        iface.messageBar().pushMessage("Erreur", "Script processing had to be stopped because errors has been detected", level=Qgis.Critical, duration=30)
        for row in rows:
            print (row)
        sys.exit()

Add your own answers!

Ask a Question

Get help from others!

© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP