0

I have a method that inserts into table NKP:

def insert_nkp(self, kniha):
    query = """
            INSERT INTO NKP SET
                id_product = %s,
                supplier_reference = %s, 
                nazev = %s, 
                nazev_orig = %s, 
                hlavni_zahlavi = %s,
                preklad_z = %s,
                zanr = %s, 
                nakladatel = %s, 
                vydani = %s,
                rozsah = %s

            """

    self.cursor.execute(query, kniha)
    self.connection.commit()

But I want to insert into NKP only if there is not existing the same id_product. I tried to add

ON DUPLICATE KEY UPDATE
id_product = VALUES(id_product)

but it did not help. What is the most elegant way how to do this, please?

vojtam
  • 1,157
  • 9
  • 34
  • 1
    Please read the answers carefully, particularly on the role of primary keys / unique indexes! – Shadow Jan 11 '22 at 09:00
  • @Shadow thank you for the reply. If I understand it correctly, I need to set `id_product` as a primary key, before I use `ON DUPLICATE KEY UPDATE` syntax? – vojtam Jan 11 '22 at 09:14
  • 1
    or as a unique index - depending on what your pk is. – Shadow Jan 11 '22 at 09:52

0 Answers0