Python Forum
Import Large CSV File into MySQL
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Import Large CSV File into MySQL
#1
Hello,

I'm trying to import one column of a large CSV file into MySQL using python 3.7. This is being done as a test run to import the rest of the columns.

For now, I can't even get the one column into the database. I was hoping to find some help.

I have setup a database with one table and only one field for the test data:
mysql> use aws_bill
Database changed

mysql> show tables;
+--------------------+
| Tables_in_aws_bill |
+--------------------+
| billing_info       |
+--------------------+

mysql> desc billing_info;
+----------+---------+------+-----+---------+-------+
| Field    | Type    | Null | Key | Default | Extra |
+----------+---------+------+-----+---------+-------+
| RecordId | int(11) | NO   |     | NULL    |       |
+----------+---------+------+-----+---------+-------+
When I run my code:
    mydb = mysql.connector.connect(user='xxxx', password='xxxxx',
                                host='xxxxx',
                                database='aws_bill')
    cursor = mydb.cursor()
    try:
        with open(source) as csv_file:
            csv_reader = csv.reader(csv_file, delimiter=',')
            sql = "INSERT INTO billing_info (RecordId) VALUES (%s)"
            for row in csv_reader:
                row = (', '.join(row))
                print(row)
                cursor.execute(sql, row)
    except:
        mydb.rollback()
    finally:
        mydb.close()
Only ONE line of the CSV column gets printed out:
python3 .\aws_billing.py
200176595756546201775238333
And nothing makes it into the database:

mysql> select RecordId from billing_info;
Empty set (0.00 sec)
If I comment out the sql insert statement: cursor.execute(sql, row)

Then all of the lines of the CSV print out:

203528424494971448426778962
203529863341009197771806423
203529974021473640029260511
203530250722634745672445063
203525214761502622966710100
203525122527782254417348410
203529365278919207614044035
...continues to the end of the file
But none of the data makes it into the database, of course. Because the SQL line is commented out. At least all of the lines of the CSV are printing out now, however, putting them into the database would be good!

Why is this happening? How can I get all the lines of the CSV into the database?
Reply
#2
In the table you have RecordId, while you try to insert InvoiceId (in the SQL statement)

Don't use try/except that hide the error - i.e. in your code you never check what error you get
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
You will also need to use commit
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Mysql and mysql.connector error lostintime 2 664 Oct-03-2023, 10:25 PM
Last Post: lostintime
Video doing data treatment on a file import-parsing a variable EmBeck87 15 2,812 Apr-17-2023, 06:54 PM
Last Post: EmBeck87
  Converted EXE file size is too large Rajasekaran 0 1,506 Mar-30-2023, 11:50 AM
Last Post: Rajasekaran
  Import XML file directly into Excel spreadsheet demdej 0 836 Jan-24-2023, 02:48 PM
Last Post: demdej
  validate large json file with millions of records in batches herobpv 3 1,260 Dec-10-2022, 10:36 PM
Last Post: bowlofred
  Pyinstaller distribution file seems too large hammer 4 2,705 Mar-31-2022, 02:33 PM
Last Post: snippsat
  Initializing, reading and updating a large JSON file medatib531 0 1,767 Mar-10-2022, 07:58 PM
Last Post: medatib531
  Mysql error message: Lost connection to MySQL server during query tomtom 6 15,988 Feb-09-2022, 09:55 AM
Last Post: ibreeden
  How from sklearn.datasets import load_diabetes change to import to CSV file Anldra12 0 1,850 Dec-25-2021, 07:20 PM
Last Post: Anldra12
  How to import file and function in another folder SriRajesh 1 3,150 Dec-18-2021, 08:35 AM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020