0

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.

In response to Martijn's response, I looked more closely at what had already been posted. Unfortunately, the one with the closest matching title ("SQLite Parameterized Update") was solved through the correction of a typographical error. So, it wasn't that helpful. If someone could point me to a post that more closely resembles my problem, I would appreciate it very much.

Thanks!

PatB

Pat B.
  • 419
  • 3
  • 12
  • Sorry: typo. Those arguments to the executable SQL statement should have been (key, value, myIndex). – Pat B. May 06 '16 at 19:37
  • 1
    Sorry, again! My screen scrolled past the article Martijn referred to! Perfect answer! Thanks for the response, and, again, my sincerest apologies. – Pat B. May 09 '16 at 16:04

0 Answers0