1

I would like to change my functions in PyQGIS from SQLite databases to Geopackages. However, the SQL statements don't quite work as I had hoped. My table has the columns: task, completed, date With SQLITE the following function works as intended (see comments) with the Geopackage I get the following error message: AttributeError: 'list' object has no attribute 'fetchedRowCount'.

What am I doing wrong?

working solution shows checkbox in the ListWidget, geopackage Version does not:

enter image description here

enter image description here

def updateList(self, date):
    self.tasksListWidget.clear()
    md = QgsProviderRegistry.instance().providerMetadata("ogr")
    con = md.createConnection( dbfile, {})

    # db = lite.connect(dbfile)
    # cursor = db.cursor()

    row = (date,)
    query = 'SELECT task, completed FROM tasks WHERE date ="%s"' %(row)
    
    # results = cursor.execute(query, row).fetchall()

    results = con.executeSql(query).fetchedRowCount()
    
    for result in results:
        item = QListWidgetItem(str(result[0]))
        item.setFlags(item.flags() | Qt.ItemIsUserCheckable)
        if result[1] == "YES":
            item.setCheckState(Qt.Checked)
        elif result[1] == "NO":
            item.setCheckState(Qt.Unchecked)
        self.tasksListWidget.addItem(item)

1 Answer 1

4

You need to use execSql instead of executeSql to use fetchedRowCount() which belongs to QueryResult class. execSql returns QueryResult. executeSql returns a list.

Because results must be iterable, you want to iterate over the data returned from the sql query. Therefore, you need the line below (remove .fetchedRowCount()):

results = con.executeSql(query)
3
  • Your right, i used the wrong executeSql. Unfortunately, I now get TypeError: 'int' object is not iterable... Any guess? I always thought Geopackage is based on a sqlite DB, too bad that you have to adapt the code anyway.
    – Omegon
    Aug 19, 2022 at 17:50
  • OK. Please check my edit. I have no chance to test the script. Aug 19, 2022 at 22:39
  • Thank you! The .fetchall() of the sqlite code irritated me. results = con.executeSql(query) without .fetchedRowCount() works.
    – Omegon
    Aug 20, 2022 at 11:53

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.