I'm trying to import data from a .xlsx file into a SQL database. Right now, I have a python script which uses the openpyxl and MySQLdb modules to
- establish a connection to the database
- open the workbook
- grab the worksheet
- loop thru the rows the the worksheet, extracting the columns I need and inserting each record into the database, one by one
Unfortunately, this is painfully slow. I'm working with a huge data set, so I need to find a faster way to do this (preferably with Python). Any ideas?
wb = openpyxl.load_workbook(filename="file", read_only=True)
ws = wb['My Worksheet']
conn = MySQLdb.connect()
cursor = conn.cursor()
cursor.execute("SET autocommit = 0")
for row in ws.iter_rows(row_offset=1):
sql_row = # data i need
cursor.execute("INSERT sql_row")
conn.commit()