0

I want to insert some values in a postgres database with a python script, but when I try it with my code below I get a syntaxerror at my first IF.

sql_string = "BEGIN IF (NOT EXISTS(SELECT * FROM table WHERE nm=name))
            BEGIN INSERT INTO table(nm, nb)
            VALUES (%s, %s) END END"
cur.execute(sql_string, (name, number))

Would be nice if someone has an idea. thanks :)

Shalev Shalit
  • 1,945
  • 4
  • 24
  • 34
trooper
  • 1
  • 1

1 Answers1

1

This approach is database agnostic.

 insert into yourtable
 (field1, field2, etc)
 select distinct value1, value2, etc
 from some_small_table
 where not exists
 (subquery to check for existing records)
Dan Bracuk
  • 20,699
  • 4
  • 26
  • 43