Search code examples
pythonpostgresqlpsycopg2

Psycopg2 : Insert multiple values if not exists in the table


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.


Solution

  • insert_query = """WITH data(name, proj_id) as (
                      VALUES (%s,%s)
                      ) 
                    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)"""
    tuple_values = (('hello',123),('hey',123))
                
    psycopg2.extras.execute_batch(cursor,insert_query,tuple_values)
    

    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.