0

I am trying to make a method which will allow me to edit existing data in an SQLite3 database. The relevant part of the code which throws an error is this:

    self.chosen = "Example_column"    # Column reference
    self.edited = "Something"         # Replacement value for chosen/chooseAccount reference
    self.chooseAccount = 1            # Primary key, in column called "ID"

    self.sql = "UPDATE Accounts_CV SET ?=? WHERE ID=?;"
    c.execute(sql, (self.chosen, self.edited, self.chooseAccount,))
    c.commit()

I get the following traceback:

Traceback (most recent call last):
  File "/Users/johntamm-buckle/Documents/python scripts/accounts/cv_main.py", line 44, in <module>
    mainLoop()
  File "/Users/johntamm-buckle/Documents/python scripts/accounts/cv_main.py", line 24, in mainLoop
    db.editEntry()
  File "/Users/johntamm-buckle/Documents/python scripts/accounts/db_handler/db_handler.py", line 125, in editEntry
    c.execute(self.sql, (self.chosen, self.edited, self.chooseAccount,))
sqlite3.OperationalError: near "?": syntax error

Am I not able to assign the field reference to a ? placeholder? If not, what is the recommended way to edit a specific ID/field reference, where both the ID and field reference are dynamic?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Gardakan
  • 1
  • 1
  • If the field name isn't user input, it's OK to `str.format` it into your query, leaving the `?`s for the values. See also http://stackoverflow.com/q/6514274/3001761 – jonrsharpe Oct 09 '15 at 13:34
  • Thank you, that solved it! Yes, the field name comes from a dict and isn't user specified. I still don't understand why the '?' placeholder didn't work though. My apologies for asking a duplicate question - I'm pretty new to this and think I needed a more specific answer to my question than I could get from questions like this - [http://stackoverflow.com/questions/25387537/sqlite3-operationalerror-near-syntax-error?rq=1](http://stackoverflow.com/questions/25387537/sqlite3-operationalerror-near-syntax-error?rq=1). – Gardakan Oct 09 '15 at 14:31
  • Because that's for values, not field names – jonrsharpe Oct 09 '15 at 14:32

0 Answers0