vendredi 6 mai 2016

Parameterizing updates in SQLite in Python [duplicate]

This question already has an answer here:

I'm attempting to create a wrapper function for an application that will update arbitrary fields in particular row in a sqlite table. As arguments to the function, I am passing a sqlite connection, an index value to search on, and a dictionary. This dictionary will contain as its keys the names of the fields to be updated and the updated entries as its values. I am then stepping through the dictionary and updating the corresponding fields in the table. So, the code that I want to use looks something like this:

def update_mytable(conn, myIndex, data_dict):

    curs = conn.cursor()
    for key, value in data_dict.iteritems():
        curs.execute("UPDATE mytable SET ?=? WHERE key =?", (key, value, index))

Unfortunately, that doesn't work. SQLite appears to complain about the field name being parameterized. Does anyone have a good work-around for this? I want the user to be able to change an arbitrary field value without having to resort to opening up a sqlite console.

Thanks!

PatB

Aucun commentaire:

Enregistrer un commentaire