database exists column values won't be added postgresql in python

22 Views Asked by At

code

cur.execute("insert into employers (id, name ...) values (%s, %s ...)", (7, "Prosenjit Das" ...) WHERE NOT EXISTS (SELECT * FROM employers WHERE VALUES = %s))
conn.commit()

How to check here my existence column value as if values are not add. For example i have already id = 7 and name "prosenjit Das" and if i want to add id = 7 and name = "something" then it won't be add into table. Just check there if id is unique. Cause most of the time i have to be execute with same values.

I'm new in Postgresql and my data will be insert from my python file that's why i can't understand how to write this logic.

Thanks.

1

There are 1 best solutions below

0
Prosenjit On

cur.execute("insert into employers (id, name ...) values (%s, %s ...)", (7, "Prosenjit Das" ...) ON CONFLICT (id) DO UPDATE SET name = excluded.name)

This is the most efficient way i've gotten and here id must be a primary key if not you will get "unique constraint matching" something like that error.