I need to insert multiple values into a table after checking if it doesn’t exist using psycopg2.
The query am using:
WITH data(name,proj_id) as (
VALUES ('hello',123),('hey',123)
)
INSERT INTO keywords(name,proj_id)
SELECT d.name,d.proj_id FROM data d
WHERE NOT EXISTS (SELECT 1 FROM keywords u2 WHERE
u2.name=d.name AND u2.proj_id=d.proj_id)
But how to format or add the values section from tuple to (‘hello’,123),(‘hey’,123) in query.
2
Answers
Here I created a tuple which holds the data to be inserted. Then execute_batch() method of the psycopg2.extras module is used to execute the insert_query with the tuple_values as the parameter. The execute_batch() method can be used to efficiently execute a parameterized query multiple times with different sets of parameters, which makes it useful for bulk inserts.
As suggested in the comment, assuming that your connection is already established as
conn
one of the ways would be: