0
我有一个psql表“inlezen”列“tagnaam”和“melding”。 现在,如果“tagnaam”等于一个变量“foutetag”,我想在字符串“meldingen”中插入一个字符串。python:psql:插入条件表
我已经尝试了一些查询,弄乱了语法,但无法让它工作。 这是我的代码:
cur.execute("INSERT INTO inlezen (melding) WHERE tagnaam == foutetag VALUES (%s)", ("Fout bij schrijven naar OPC Server",))
conn.commit()
但它给错误:
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...
有谁知道什么地方错了这段代码?
在此先感谢!马蒂亚斯艾丁格的回答后
编辑:
我已经改变了代码和错误改变了一点点:
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...
^
现在至少“看到”变量的内容。但是,变量周围有单引号。我不确定他们是否应该在那里。 如果我打印变量“foutetag”它只是显示:
Bakkerij.Device1.DB100INT8
,就像它在PSQL表。
我这是怎么产生的变量:
foutetag = [item[0] for item in taglistwaardennieuw]
foutetag = (", ".join(foutetag))
难道还有什么错我产生可变的方式吗?
我已经在我的OP中回复。 – RobbeM
已更新。你的SQL语句从一开始就生病了 –
谢谢,这个工作! – RobbeM