I have a nested dictionary "sample_dict and a few Parameters in Python :
import pandas as pd
#Parameters
Condition_Name='ID_0.5_365D'
Insert_Date='2021-05-19 00:00:00'
sample_dict=\
{pd.Timestamp('2015-01-02 00:00:00'): {'navi.o_efx': 180.0,
'navi.o_qrvo.o': 180.0,
'navi.o_stm.n': 180.0,
'qrvo.o_efx': 1.0,
'qrvo.o_stm.n': 1.0},
pd.Timestamp('2014-12-31 00:00:00'): {'navi.o_efx': 179.0,
'navi.o_stm.n': 179.0}}
which, for a given date, has varying keys and values.
The structure of the MySQL table "sample_table" intended for insertion is as follows:
CREATE TABLE sample_table (
Date datetime DEFAULT NULL,
Pair_Name varchar(255) DEFAULT NULL,
Condition_Name varchar(255) DEFAULT NULL,
Condition_Value double(16, 4) DEFAULT NULL,
Insert_Date datetime DEFAULT NULL
)
ENGINE = INNODB,
AVG_ROW_LENGTH = 159,
CHARACTER SET latin1,
COLLATE latin1_swedish_ci;
How can a MySQL insert query be created by using for loops in Python by merging the "sample_dict" and "Parameters"? For example, the output query should be created in the below shown format:
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2015-01-02 00:00:00','navi.o_efx','ID_0.5_365D',180,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2015-01-02 00:00:00', 'navi.o_qrvo.o','ID_0.5_365D',180,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2015-01-02 00:00:00', 'navi.o_stm.n','ID_0.5_365D',180,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2015-01-02 00:00:00', 'qrvo.o_efx','ID_0.5_365D',1,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2015-01-02 00:00:00', 'qrvo.o_stm.n','ID_0.5_365D',1,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2014-12-31 00:00:00','navi.o_efx','ID_0.5_365D',179,'2021-05-19 00:00:00');
INSERT INTO sample_table(Date, Pair_Name, Condition_Name,Condition_Value,Insert_Date) VALUES ('2014-12-31 00:00:00','navi.o_stm.n','ID_0.5_365D',179,'2021-05-19 00:00:00');
The final inserts in the "sample_table" would look like this:
