I want to INSERT INTO MySQL or UPDATE MySQL if exist without having to run 2 different queries.
I have checked Create if an entry if it doesn't exist, otherwise update?
INSERT INTO table (a) VALUES (0) ON DUPLICATE KEY UPDATE
but it only works if you check doubles on the key. The issue is I want to check on another field that is not the key, but yet has so stay unique. I want to ADD or UPDATE on checking the 'symbol' in my dictionary:
The data I get is (example)
forex = [{'symbol': 'EURUSD', 'price': '1.06763000'}, {'symbol': 'GBPEUR', 'price': '0.90339600'}, {'symbol': 'EURJPYC', 'price': '0.000011200'}]
The INSERT INTO I want to adapt is:
mycursor.executemany(f"INSERT INTO `param_forex` (Ticker,Price)
VALUES ( %(symbol)s, %(price)s)", forex)
mydb.commit()
I do NOT want to use the REPLACE because of the auto increment field (REPLACE deletes and inserts)
The column I am checking for duplicates is Ticker
in the table and symbol in the dictionary.
I am stuck. Is there a solution?