我目前有一个代码,它的作用是批量插入,但会先将表截断。
cnn = self.connect()
# Initialize a string buffer
sio = StringIO()
# Write the Pandas DataFrame as a csv to the buffer
sio.write(df.to_csv(index=None, header=None, sep=';'))
# Be sure to reset the position to the start of the stream
sio.seek(0)
# Copy the string buffer to the database, as if it were an actual file
cursor = cnn.cursor()
cursor.execute(f'TRUNCATE {tabla}')
cursor.copy_from(sio, tabla,
columns=df.columns, sep=';', null="")
cnn.commit()
但是现在我希望保留历史记录,以便在有新记录的情况下输入,但如果有重复收入,则按日期,时间和工厂进行更新
最优化和推荐的方法是什么?从而获得用于各种数据帧的可重用代码。
示例:
Dataframe1, with these columns = date;time;plant;value
Dataframe2, with these columns = date;time;plant;value1;value2
Dataframe3, with these columns = date;time;value
是否可以使代码适用于通过参数传递给具有不同结构的函数的数据帧?