2

Is there any way to turn my excel workbook into MySQL database. Say for example my Excel workbook name is copybook.xls then MySQL database name would be copybook. I am unable to do it. Your help would be really appreciated.

1
  • There always is a way, but what have you already tried and what error did you encounter? It's easier to help you out with concrete errors than it is to just type in an entire solution. Commented Jul 20, 2020 at 7:20

1 Answer 1

1

Here I give an outline and explanation of the process including links to the relevant documentation. As some more thorough details were missing in the original question, the approach needs to be tailored to particular needs.

The solution

There's two steps in the process:

1) Import the Excel workbook as Pandas data frame

Here we use the standard method of using pandas.read_excel to get the data out from Excel file. If there is a specific sheet we want, it can be selected using sheet_name. If the file contains column labels, we can include them using parameter index_col.

import pandas as pd
# Let's select Characters sheet and include column labels
df = pd.read_excel("copybook.xls", sheet_name = "Characters", index_col = 0)

df contains now the following imaginary data frame, which represents the data in the original Excel file

first   last
0   John    Snow
1   Sansa   Stark
2   Bran    Stark

2) Write records stored in a DataFrame to a SQL database

Pandas has a neat method pandas.DataFrame.to_sql for interacting with SQL databases through SQLAlchemy library. The original question mentioned MySQL so here we assume we already have a running instance of MySQL. To connect the database, we use create_engine. Lastly, we write records stored in a data frame to the SQL table called characters.

from sqlalchemy import create_engine
engine = create_engine('mysql://USERNAME:PASSWORD@localhost/copybook')
# Write records stored in a DataFrame to a SQL database
df.to_sql("characters", con = engine)

We can check if the data has been stored

engine.execute("SELECT * FROM characters").fetchall()

Out:
[(0, 'John', 'Snow'), (1, 'Sansa', 'Stark'), (2, 'Bran', 'Stark')]

or better, use pandas.read_sql_table to read back the data directly as data frame

pd.read_sql_table("characters", engine)

Out:
index   first   last
0   0   John    Snow
1   1   Sansa   Stark
2   2   Bran    Stark

Learn more

No MySQL instance available?

You can test the approach by using an in-memory version of SQLite database. Just copy-paste the following code to play around:

import pandas as pd
from sqlalchemy import create_engine
# Create a new SQLite instance in memory
engine = create_engine("sqlite://")
# Create a dummy data frame for testing or read it from Excel file using pandas.read_excel
df = pd.DataFrame({'first' : ['John', 'Sansa', 'Bran'], 'last' : ['Snow', 'Stark', 'Stark']})
# Write records stored in a DataFrame to a SQL database
df.to_sql("characters", con = engine)
# Read SQL database table into a DataFrame
pd.read_sql_table('characters', engine)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks I have done below one: filename=str(sys.argv[1]) loc = filename+'.xlsx' output=pd.ExcelFile(loc) engine = sa.create_engine('mysql+pymysql://sourav:@localhost:3306') create_database = "CREATE database IF NOT EXISTS " + filename cursor.execute(create_database)
@Sourav_Bharadwaj Did you manage to solve the issue you were facing?
SQLAlchemy attempts to import MySQLdb and generates a modulenotfound error. PyCharm has MySQLdb in it's library of libraries to install, but there's absolutely NO information on the library AT ALL. No version, no author, nothing! And when you try to install it, you get an error message since the version information is missing.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.