Import data from Excel into MySQL using Python

Import data from Excel into MySQL using Python


In this video I demonstrate how to create a Python script that will import data from Excel into MySQL using the xlrd library.

Blog article - http://mherman.org/blog/2012/09/30/im...
Closed Caption:

Today I’m going to show you how to import data from Excel into MySQL using Python.
Start by installing the xlrd library, which is used for extracting data from Excel spreadsheets.
Keep in mind, that xlrd only supports the classic XLS file
not the XML-format XLSX file.
You also need to install the MySQLdb module in order to connect to the database.
Both are easy to install.
Links to both of their download pages are available on my blog
which you can find a link to in the description of this video.
After installing xlrd and MySQLdb, create the table that you’d like to use in your database if it’s not already created.
My table looks like this.
The primary key is an auto-incremented id. The other 13 columns are a mixture of integers and strings
which will be populated by the Excel data.
Now to create the Python code, I like to start with writing out pseudo code, in plain English
detailing what it is that I’d like the code to do.
Then I turn the pseudo code into comments and write the actual code.
For time’s sake, I already wrote out the pseudo code and commented it out, which you can see here.
And the code is written as well, so I will just be copying and pasting it over, explaining each part.
Start by importing xlrd and MySQLdb to make them available.
To open the workbook, set a variable, book, and set it equal to xlrd.open_wordbook
and then enter the path to and file name of your XLS file.
Then to access a sheet
you can either use its location in the Excel file with the sheet_by_index method
in the Excel file with the sheet_by_index method
or by the sheet’s name with the sheet_by_name method.
Next you need to establish a MySQL connection.
To do that set a variable, database, equals MySQLdb.connect.
Enter your full host name, username, password, and then specify the database.
There are several other parameters that can be used as well like port if it's different than the default MySQL port.
You can also set the path to configuration file using read_default_file
so you don’t have to expose your username or password in the code.
Next you want to get the cursor.
Basically that's used to read the database, line by line, so that you can perform a query.
And so you just want to set a variable, cursor, equals database.cursor().
Then you want write out the INSERT INTO SQL statement .
Alright now you need to create a FOR loop.
Which will iterate through each row in the XLS file.
This creates an array called r.
And then you establish the cells here that you want to use for the iteration
Now you want to assign values to match the placeholders.
And this is placed in the loop as each row in the Excel file is different.
Then you want to execute the SQL query.
Enter cursor.execute
then enter the variable names for the query
and assigned values.
Now exit the loop
and close the cursor.
cursor.close closes the previously opened cursor. Make sure to do this before you close the database connection
so you can still commit the data manipulations to the database
using database.commit
Finally, close the the connection to the database .
And then print results.
Here I'm just saying goodbye.
And then I have variables that will calculate the number of columns and the number of rows in the Excel file .
Now let’s test this out.
I’m going to open up the MySQL console.
So you can see what data is already in the table.
So it's empty.
Let's run the file now.
Open up Command Prompt.
CD on over to where the newly created Python file is, and then just run the file.
And I just imported 13 columns and 3160 rows of data from Excel to MySQL.
Let’s just verify that the table is now populated with data.
Which it is.
Alright more fun stuff with MySQL and Python coming soon.
Thanks for watching.

Video Length: 07:05
Uploaded By: Michael Herman
View Count: 29,404

Related Software Products
Excel-to-MySQL
Excel-to-MySQL

Published By:
Intelligent Converters

Description:
Excel-to-MySQL is a program to convert MS Excel spreadsheet into MySQL database. Each worksheet becomes a table, each row becomes a record and each cell becomes a field.BRBR License InformationBRBR Single-User License allows clients to install the software product on exactly one (1) computer system. Client can store a backup copy of the software but cannot use two copies of the software at any one time. Single-User License does NOT allow to make use of the software through a ...


Related Videos
Import Excel to MySQL Database
Import Excel to MySQL Database

Take a Microsoft Excel Spreadsheet and import it into a MySQL Database using CSV files and PHPMyAdmin. hr / bClosed Caption:/b hello everyone and welcome to our tutorial on how to import a Microsoft Excel spreadsheet into a my sequel database first just like to introduce myself my name is matter of wine and I work for the office of instructional design here in Northern Kentucky University I work for Mike lively who is ...
Video Length: 10:53
Uploaded By: Michael Lively
View Count: 432,577

Connecting SQL Tables and data in Excel spreadsheets
Connecting SQL Tables and data in Excel spreadsheets

For more videos on technology, visit http://www.Techytube.com By Jayanth@techytube SQL server is a powerful database platform, this means that it can also be complex to understand and work with. However the major users of the data in the database are still a non-technical business user. A key problem that most business users face when it comes to working with SQL Server is the dependency on an IT professional to query and return the data in a CSV format or Excel sheet. ...
Video Length: 03:08
Uploaded By: techytube
View Count: 112,465

How to import data from Excel to MySQL database
How to import data from Excel to MySQL database

This tutorial show the process to import data directly from a Excel document to MySQL database server using the smart copy feature available in the DreamCoder for MySQL.
Video Length: 01:16
Uploaded By: sqldeveloper
View Count: 111,941

How to Set ODBC MySQL Driver
How to Set ODBC MySQL Driver

Follow this step by step tutorial to learn how to setup MySql ODBC driver for windows and how it is configured. Don't forget to check out our site http://howtech.tv/ for more free how-to videos! http://youtube.com/ithowtovids - our feed http://www.facebook.com/howtechtv - join us on facebook https://plus.google.com/103440382717658277879 - our group in Google+ ODBC stands for Open Database Connectivity. It is used to provide a connection for ...
Video Length: 02:09
Uploaded By: Internet Services and Social Networks Tutorials from HowTech
View Count: 78,110

How to Easily Convert an Excel Spreadsheet to MySQL
How to Easily Convert an Excel Spreadsheet to MySQL

A short screencast of how to use SQL Converter 2 for Excel to convert an Microsoft Excel spreadsheet to MySQL (2006). More at www.sqlconverter.com. hr / bClosed Caption:/b hello my name is Frank Rihanna and i'm going to show you just how easy it is to take any data from excel to a mysql database using SQL converter to do this i'm going to use excel 2000 with SQL converter to installed you'll notice the presence of the SQL menu i'm ...
Video Length: 03:55
Uploaded By: SQLConverter
View Count: 68,732

Php :- Import CSV data to mysql database
Php :- Import CSV data to mysql database

Hey guys i will show you how to import data from a csv file to mysql database, it can be very helpful while creating professional scripts for business needs hr / bClosed Caption:/b hey whats up guys welcome back to the too late . com today i will be showing you how do you see a speed functionality into your PHP script because if you will ever work for a business client i believe 90 portion of them will ask you to integrate csv files ...
Video Length: 09:11
Uploaded By: tutlage
View Count: 52,456

MySQL for Excel Introduction
MySQL for Excel Introduction

Quick video that shows the main features of MySQL for Excel. Please subscribe to our Youtube channel: http://www.youtube.com/mysqlchannel Post your questions about MySQL for Excel on our forum: http://forums.mysql.com/list.php?172 Find more about MySQL on Windows at our blog: https://blogs.oracle.com/mysqlonwindows/ Like our MySQL Facebook page: http://facebook.com/mysql Follow us on Twitter at:br ...
Video Length: 04:42
Uploaded By: MySQL
View Count: 42,072

Visualizing Data using MySQL and Excel 2013
Visualizing Data using MySQL and Excel 2013

In this video I show how to pull data from MySQL using MS Query and then explain best how to visualize the data. Oh, and you'll see a cool new Excel 2013 feature as well. hr / bClosed Caption:/b With Edward Tuffte in mind I'd like to show you an interesting way of visualizing data in a Histogram. So, I am going to use the ODBC connection to pull the data from my database. So, start by opening up MS Query. Select your data source.br ...
Video Length: 05:46
Uploaded By: Michael Herman
View Count: 39,471

Php Export mysql data to csv file :- Part 1
Php Export mysql data to csv file :- Part 1

So finally after importing data it's time to export data from mysql and insert it to csv file.Stay tuned to get more awesome tuts Playlist Link :- http://www.youtube.com/playlist?list=... hr / bClosed Caption:/b hey welcome back guys collectively 2.com and as a promise i will be showing you how to explore data from mysql database and inserted to a CSV file so what we're going to do out here inside the folder we had like.we ...
Video Length: 08:25
Uploaded By: tutlage
View Count: 33,470

Exporting MySQL Database Table to Excel
Exporting MySQL Database Table to Excel

In this short video we export a MySQL database table to a Microsoft Excel 2000 spreadsheet using CPanel and phpMyAdmin. hr / bClosed Caption:/b hello and welcome to our tutorial on exporting a sequel database or database table to an XML spreadsheet my name is matter of wine and I'm with the with nku Northern Kentucky University's office of instructional design and this just be a really short tutorial here first you want to ...
Video Length: 03:54
Uploaded By: Michael Lively
View Count: 33,158

Copyright © 2025, Ivertech. All rights reserved.