Stack Exchange
python: psql: insert into table on conditions
I have a psql table "inlezen" with columns "tagnaam" and "melding".
Now I want to insert a string into column "meldingen" if "tagnaam" equals a variable "foutetag".
I have tried some queries, messed with the syntax but couldn't get it to work.
This is my code:
cur.execute("INSERT INTO inlezen (melding) WHERE tagnaam == foutetag VALUES (%s)", ("Fout bij schrijven naar OPC Server",)) 
conn.commit() 

But it gives errors:
Traceback (most recent call last):
  File "OPCSchrijvenLezen.py", line 71, in <module>
    cur.execute("INSERT INTO inlezen (melding) WHERE tagnaam == foutetag VALUES (%s)", ("Fout bij schrijven naar OPC Server",)) 
psycopg2.ProgrammingError: syntax error at or near "WHERE"
LINE 1: INSERT INTO inlezen (melding) WHERE tagnaam == foutetag VALU...

Does anyone know what's wrong with this piece of code?
Thanks in advance!
Edit after Mathias Ettinger's answer:
I've changed the code and the error changed a little bit:
Traceback (most recent call last):
  File "OPCSchrijvenLezen.py", line 72, in <module>
    cur.execute("INSERT INTO inlezen (melding) WHERE tagnaam = %s VALUES (%s)", (foutetag, "Fout bij schrijven naar OPC Server",))
psycopg2.ProgrammingError: syntax error at or near "WHERE"
LINE 1: INSERT INTO inlezen (melding) WHERE tagnaam = 'Bakkerij.Devi...
                                      ^

Now it "sees" the content of the variable at least. However, there are single quotation marks around the variable. I'm not sure if they should be there.
If I print the variable "foutetag" it just shows:
Bakkerij.Device1.DB100INT8

, like it is in the psql table.
This is how I generate the variable:
foutetag = [item[0] for item in taglistwaardennieuw]
foutetag = (", ".join(foutetag))

Could there be anything wrong the way I generate the variable?

If foutetag is a variable, then you need to treat it like so:
cur.execute("INSERT INTO inlezen (melding) WHERE tagnaam=%s VALUES (%s)", (foutetag, "Fout bij schrijven naar OPC Server",))

Also note the single = in the WHERE clause.
However the INSERT statement does not accept a WHERE clause, as it will produce a new row from scratch. If you want to update your value, use:
cur.execute("UPDATE inlezen SET melding=%s WHERE tagnaam=%s", ("Fout bij schrijven naar OPC Server", foutetag))


I've replied in my OP.

Updated. Your SQL statement was ill formed from the beginning
