How to insert only elements that do not exist?

1

I have a python script that looks for elements of one database and saves it in another, so when I run the script it is duplicating the data.

sql = "INSERT INTO 'alarm' ('data', 'seconds', 'culprit', 'status') VALUES (%s, %s, %s, %s)

Is there anyway I can change this INSERT to insert only what is not contained.

    
asked by anonymous 30.10.2017 / 15:48

1 answer

2

Which of the primary keys in your alarm table? It is what defines whether your records are duplicates. This part is essential . From there, you can do the insertion in several ways:

  • INSERT IGNORE INTO ...: Ignore duplicate records (see here )
  • INSERT INTO ... ON DUPLICATE KEY UPDATE: Refreshes specific fields if the key is duplicated (see here )
  • REPLACE INTO ...: If the registry is duplicated, it deletes and inserts the new line.
  • 30.10.2017 / 17:23