Skip to content
Advertisement

update with csv file using python

I have to update the database with the CSV files. Consider the database table looks like this: database table

The CSV file data looks like this:

enter image description here

As you can see the CSV file data some data modified and some new records are added and what I supposed to do is to update only the data which is modified or some new records which are added.

In Table2 the first record of col2 is modified.. I need to update only the first record of col2(i.e, AA) but not the whole records of col2.

I could do this by hardcoding but I don’t want to do it by hardcoding as I need to do this with 2000 tables.

Can anyone suggest me the steps to approach my goal.

Here is my code snippet..

Advertisement

Answer

Since I do not have data similar to you, I used my own DB. The schema of my books table is as follows:

And the table looks like this:

So, my approach is to create a new temporary table with the same schema as the books table from the CSV using python. The code I used is as follows:

Which creates a table like this:

Now, you just need to use the update in MySQL using INNER JOIN to update the values you want to update in your original table. (in my case, ‘books’).

Here’s how you’ll do this:

This query will update the values in table books from the table temp that I’ve created using the CSV.

You can destroy the temp table after updating the values.

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement