Migrating from Excel to MySQL
I have not written about MySQL and Python for a long time, so today I tell you how to migrate a table that you have in Excel to MySQL, all this using Python. And all, in less than 50 lines. This entry today is a totally different approach to using MySQL for Excel, which I already told you about a while ago.
For the solution I propose, I used SqlAlchemy, and pandas. Obviously, you need to have a MySQL server. .
SqlAlchemy is an Object Relational Model library, this is ORM, which facilitates the writing of Python code that will be the ORM that is responsible for translating it into the language of the database. I have not used it in many projects, and the truth is, it is a little uncomfortable for me to write Python code that will be executed in the database, as well as not to use SQL, and to do it in a Python language, adapted to the imposed rules by SqlAlchemy. It’s not that it’s a bad thing, but it’s more of a personal mania, and in fact, I have not used it on almost any project.
However, today is one of those moments where you can get good performance from SqlAlchemy!
Pandas is the library par excellence to work with data in Python. Although with this library I have worked on multiple projects, the truth is that I have named the blog little. But hey, it should ever be the first!
Before continuing, I assume that you have installed Anaconda, the best distribution of Python (to me, is the one I like and I use). In case you do not have it, I recommend that you install the python MySQL client with the command:
pip install mysqlclient
The fact of installing this MySQL client waas due to an error I was receiving, because I don’t have installed the module:
Error loading MySQLdb module: No module named 'MySQLdb'
So, if you receive this error, you now know what do you need in order to solve it: install the MySQL client.
The second thing to consider is that you need to have installed SqlAlchemy, (it is included in Anaconda !!) and you can check it with:
The third consideration is that, to work with UTF-8, it is necessary to have the correct connection string, and in fact, it was good for me to read this answer: https://stackoverflow.com/questions/16394332/unicodeencodeerror-latin-1-codec-cant-encode-characters-in-position-0-1-ordi.
The code for migrating from Excel to MySQL
We have all the requirements ready, now you only need to modify the variables to locate the route, the Excel file, the tab that will become a table, and for MySQL, in addition to the connection data, how you want your new table to be called.
The code is this:
# coding: utf-8 <br /># -------------------------------------------------------------------- <br /># Name: Excel 2 MySQL # Copyright: (c) ManejandoDatos.es 2019 <br /># -------------------------------------------------------------------- __APP__ = "Excel 2 MySQL - Version %s (%s)" <br />__VERSION__ = "1.0.0" <br />__FECHA__ = "7 Marzo 2018" <br />nl = "\n" <br />print((__APP__ + nl) % (__VERSION__, __FECHA__)) <br />ruta = r"C:\" fichero = "file.xlsx" <br />solapa = "sheet_name" <br />tabla = "table_name" <br />host = "localhost" <br />user = "user" <br />passwd = "***" <br />database = "your_db" <br />try: <br /> import sys <br /> import pandas as pd <br /> from sqlalchemy import create_engine<br />except Exception as e: <br /> print(" *** ERROR FATAL *** Error inicial al cargar librerias!! ") <br /> print(e) sys.exit(0) def excel2mysql(): <br />try: <br /> try: <br /> xls = pd.ExcelFile(ruta + "\\" + fichero) <br /> x1 = xls.parse(solapa) <br /> x1.columns = map(str.lower, x1.columns) <br /> # Columnas a lower <br /> engine = create_engine('mysql://%s:%s@%s/%s?charset=utf8&amp;amp;amp;use_unicode=1' % (user, passwd, host, database), encoding='utf-8') <br /> with engine.connect() as conn, conn.begin(): <br /> x1.to_sql(tabla, conn, if_exists='replace') <br /> print ("Migración terminada!") <br />except Exception as f: <br /> print('Imposible conectar con Base de datos %s!! Revise datos de conexión!!' % (host)) <br /> print(f) <br />except Exception as e: <br /> print('ERROR!! Imposible abrir fichero') <br /> print(e) <br /><br />if __name__ == '__main__':<br /> excel2mysql()
Easy, isn’t it? If you like it, you can help me for mainining this blog with a beer!
As additional information, I recommend that you use the echo = True option in create_engine so that you can see all the SQL statements that are taking place in the process, although when you have become accustomed to this little script, it will be dispensable! Como información adicional, te recomiendo que uses la opción echo=True en create_engine para que vayas viendo todas las instrucciones SQL que van teniendo lugar en el proceso, aunque cuando te hayas acostumbrado a este pequeño script, te será prescindible!
También quiero hacer notar que hay que indicar que vas a trabajar con UTF-8 tanto en la cadena de conexión, como en la opción encoding de create_engine.