I have created a database using phpmyadmin called test that has one table called client_info.
The table in that database is empty (as shown in the attached image)
On the other side, I have written a code in python that read several CSV files and then extract specfic columns into dataframe called Client_Table1. This dataframe contains several rows and 3 columns
so far I have written this code :
import pandas as pd
import glob
path = r'D:SWAMERP_Data' # Path of Data
all_files = glob.glob(path + "/*.csv")
li = []
for filename in all_files:
df = pd.read_csv(filename,sep=';', index_col=None, header=0,encoding='latin-1')
#df = pd.read_csv(filename, sep='t', index_col=None, header=0)
li.append(df)
ERP_Data = pd.concat(li, axis=0, ignore_index=True)
# rename the columns name
ERP_Data.columns = ['Client_ID', 'Client_Name', 'FORME_JURIDIQUE_CLIENT', 'CODE_ACTIVITE_CLIENT', 'LIB_ACTIVITE_CLIENT', 'NACE',
'Company_Type', 'Number_of_Collected_Bins', 'STATUT_TI', 'TYPE_TI', 'HEURE_PASSAGE_SOUHAITE', 'FAMILLE_AFFAIRE',
'CODE_AFFAIRE_MOUVEMENT', 'TYPE_MOUVEMENT_PIECE', 'Freq_Collection', 'Waste_Type', 'CDNO', 'CDQTE',
'BLNO', 'Collection_Date', 'Weight_Ton', 'Bin_Capacity', 'REF_SS_REF_CONTENANT_BL', 'REF_DECHET_PREVU_TI',
'Site_ID', 'Site_Name', 'Street', 'ADRCPL1_SITE', 'ADRCPL2_SITE', 'Post_Code',
'City', 'Country','ZONE_POLYGONE_SITE' ,'OBSERVATION_SITE', 'OBSERVATION1_SITE', 'HEURE_DEBUT_INTER_MATIN_SITE',
'HEURE_FIN_INTER_MATIN_SITE', 'HEURE_DEBUT_INTER_APREM_SITE', 'HEURE_DEBUT_INTER_APREM_SITE', 'JOUR_PASSAGE_INTERDIT', 'PERIODE_PASSAGE_INTERDIT', 'JOUR_PASSAGE_IMPERATIF',
'PERIODE_PASSAGE_IMPERATIF']
# extracting specific columns
Client_Table=ERP_Data[['Client_ID','Client_Name','NACE']].copy()
# removing duplicate rows
Client_Table1=Client_Table.drop_duplicates(subset=[ "Client_ID","Client_Name" , "NACE"])
I would like to Write Pandas DataFrame (i.e., Client_Table1) into the existed MySQL Database (i.e., test) specfically in the table client_info.
the expected output in MySQL Database (i.e., **test**), would be
writing the **Client_ID** column (i.e., values of **Client_ID** column) into MySQL Database column **code**
writing the **Client_Name** column into MySQL Database column **name**
writing the **NACE** column into MySQL Database column **nac**
3
Answers
try this doc
you have to create a connection, and then write data to your database.
In Ideal situation for any database operation you need:
That is just a concept.
That is just a concept. I can not test the code I have written. There might be some error. You might need to debug it. For example data type miss match as I am considering all row as string with %s. Please read more in detail here.
Edit Based on Comment:
You can create separate methods for each table with a sql statement and then run them at the end. Again that is just a concept and can be generalised more.
I wrote this answer for another user this morning and thought it might help you as well.
This code reads from a CSV file and writes to MySQL using
pandas
andsqlalchemy
.Let me know if you need any tweaks to help you more specifically.
Answer:
The code below performs the following actions:
engine
(connection) created.DataFrame
DataFrame
used to store data into MySQL.Source File (
comma_test.csv
):Unedited Data:
Edited Data:
Queried from MySQL:
Acknowledgement:
This is a long-winded approach. However, each step has been broken down intentionally to clearly show the steps involved.