[GIS] QGIS Python SQLite using variable

pythonsqlite

I have been looking for answers and working on this for many hours.
In the spatialite database table nodes, id is an integer.

    idValToNode = 854

    sql =  'UPDATE nodes SET obsolete = 0 WHERE id = ?'
    cur.execute(sql, idValToNode)

ValueError: parameters are of unsupported type

    idValToNode = 854

    sql =  'UPDATE nodes SET obsolete = 0 WHERE id = ?'
    cur.execute(sql, str(idValToNode))

ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 3 supplied.

    idValToNode = 854

    sql =  'UPDATE nodes SET obsolete = 0 WHERE id = ?'
    cur.execute(sql, idValToNode,)

ValueError: parameters are of unsupported type

If I place the 854 inside the sql statement and remove the variable, it works fine.
I need to use a variable. Please help. What am I missing here?

Best Answer

Looking at the docs, your parameter needs to be a tuple, and Python expects single-element tuples to be constructed like this: (idValToNode,). That would make your cur.execute() function look more like this:

cur.execute(sql, (idValToNode,))