I have a MySQL table that is constantly updated every hour. Given an existing MySQL table below:
date time price
11/6/2019 7:10 4.56
11/6/2019 7:15 5.01
11/6/2019 7:20 4.87
11/6/2019 7:25 4.54
Two questions:
How can I avoid inserting duplicate rows if there is another entry on 11/6/2019 at 7:25 with the same 4.54 price?
How can I insert the latest price if there is a new insert entry for 11/6/2019 at 7:25 with the latest price of 5.02? (this latest entry, same date/time but different price will replace the existing)
I am new to MySQL and I tried Googling but am unable to find any clear answers - it is making me more confused rather. It seems like in this case, using primary key/unique key is not an option.
I am using Pandas/sqlalchemy/MySQLdb. My code is below
import pandas as pd
import numpy as np
import MySQLdb
from sqlalchemy import create_engine
df = pd.read_csv('summary_1.csv')
price_df = df[['date','time','price']]
engine = create_engine('mysql+mysqldb://root:python@localhost:3306/testdb2',
echo = False)
df.to_sql(name = 'price', con=engine, if_exists = 'append', index = True)
Anyone can enlighten me? Let me know if you need more information. Thank you!