-1

I'm a new coder around here and I've been struggling to find out why there is a syntax error in my code but I cannot understand why.

 c.execute("UPDATE piesemagazin SET (?,?,?,?,?) WHERE IdProdus=?",(id, nume, tip, numar, pret,id))

I have tried to replace it with INSERT OR REPLACE , also tried the .format type but none of these seem to work.

GMB
  • 216,147
  • 25
  • 84
  • 135
  • 2
    You have to specify the columns to change in the SQL query. See https://www.sqlite.org/lang_update.html – Michael Butscher Apr 29 '23 at 21:14
  • It is specified, IdProdus is the integer primary key by which im trying to identify a column – Timpu Eusebiu Apr 29 '23 at 21:18
  • 2
    With the `WHERE` clause with `IdProdus=?` you specify the row (the record to change) but the new values for the columns of the record must be explicitly mapped each to a column. – Michael Butscher Apr 29 '23 at 21:24
  • i specified them in the tuple – Timpu Eusebiu Apr 29 '23 at 21:29
  • i have resolved the issues i just had to rewrite it like this : query = "INSERT OR REPLACE INTO piesemagazin (IdProdus, NumeProdus, TipProdus, StocProdus, PretProdus) VALUES (?, ?, ?, ?, ?)" c.execute(query, (id, nume, tip, numar, pret)) – Timpu Eusebiu Apr 29 '23 at 21:43

1 Answers1

0

I believe that you could use the following:-

c.execute("UPDATE piesemagazin SET id=?,nume=?,tip=?,numar=?,pret=? WHERE IdProdus=?",(id, nume, tip, numar, pret,id))
  • that is you specify SET followed a comma separated list of the column and the value, as an expression, noting that you cannot bind identifiers (column,tables names etc) so the ?'s are replaced by the bound values

  • there would be no need to include the id column in the SET clause, as it will be unchanged as the variable id is the same so you are effectively saying change the id to the value of the id that was found by the WHERE clause.

This is the path (highlighted) as applied to the SQLite documentation (noting that the encircled blue code is csv of column/values):-

enter image description here

MikeT
  • 51,415
  • 16
  • 49
  • 68