1

Firstly, I know that there are already posts on here with the similar titles, I have looked at the answers to those questions but still can't figure out a solution from those answers. (My problem is slightly different to the rest).

I need to be able to update the value of a field for a specific record in my sqlite database. One of the posts I have used to help me with this problem is this.

I have used the same formatting in my code here:

updateCustomerTable = c.execute("UPDATE customerTable SET {} = ? WHERE customerID = ?").format(age), (newAge.get(),) , (customerID.get(),)

but I am getting an error message:

sqlite3.OperationalError: unrecognized token: "{"

Why is the curly bracket not allowed when the developer has used it in the answer for the other post?

Community
  • 1
  • 1
JoeW373
  • 59
  • 1
  • 11

1 Answers1

2

Here (code reformated for readability):

c.execute(
    "UPDATE customerTable SET {} = ? WHERE customerID = ?"
    ).format(age)

so in fact you are calling .format() on the result of c.execute(), not on your SQL query string. It would actually raise an AttributeError if the query was valid SQL FWIW.

What you want is:

sql = "UPDATE customerTable SET {} = ? WHERE customerID = ?".format(age)  
c.execute(sql, ...)
bruno desthuilliers
  • 75,974
  • 6
  • 88
  • 118
  • I've put the arguments in that retrieve the results into the execute line, so c.execute(sql, (newAge.get(),) , (customerID.get(),)) but that means there's 3 arguments where 2 are given? Am i supposed to put something else into that line? – JoeW373 Dec 07 '16 at 11:10
  • Is this question resolved? As noted, the `.format()` method must be operated on string statement with query parameters passed in one additional argument as either list or tuple. – Parfait Dec 12 '16 at 21:41