How to use "WHERE NOT EXISTS in INSERT STATEMENT" - Mailing list psycopg

From Serge Christian Ibala
Subject How to use "WHERE NOT EXISTS in INSERT STATEMENT"
Date
Msg-id CABsq6iyHGcH0EN9t+V5cvOyZh0PMHpdLVE6P_tUbgFaRjyZqGQ@mail.gmail.com
Whole thread Raw
Responses Re: How to use "WHERE NOT EXISTS in INSERT STATEMENT"
Re: How to use "WHERE NOT EXISTS in INSERT STATEMENT"
List psycopg
Hello All,

I want to insert some data in my data base (postgrsql) using Python but i want to check first that the row does not already exist.
The normal command is:

cur.execute("INSERT INTO COMPANYLIST (ID, NAME, AGE, ADDRESS, SALARY) \      
VALUES (1, 'Paul', 32, 'California', 20000.00)");

I get the error below when running the code twice.
===================================================
psycopg2.IntegrityError: duplicate key value violates unique constraint "companylist_pkey"
DETAIL:  Key (id)=(1) already exists.
====================================================

I have tried to replace it by:

cur.execute("INSERT INTO COMPANYLIST (ID, NAME, AGE, ADDRESS, SALARY) \
WHERE NOT EXISTS (SELECT 1 FROM table WHERE id='1')\
VALUES (1, 'Paul', 32, 'California', 20000.00)");

It does not work. Can anybody please help?
Kind Regards,
Christian

psycopg by date:

Previous
From: Alan Evans
Date:
Subject: PQntuples for older version (2.2.1) of psycopg2 with ctypes
Next
From: Jonathan Rogers
Date:
Subject: Re: How to use "WHERE NOT EXISTS in INSERT STATEMENT"