Azure SQL DB Read & Write with Pandas for Dummies

Tina Bu
2 min readMar 7, 2019

--

(Source)

This post is a simple example of how to connect to an Azure SQL Server from Python and how to read data and write results back with Pandas.

First, to be able to connect to the database server, in the same folder as your code, you need to create a file called config.py with your JSON formatted credentials.

config.py

Then to be able to connect to SQL Server, make sure to have ODBC installed locally. If you have a MacBook, do:

/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)" brew tap microsoft/mssql-release https://github.com/Microsoft/homebrew-mssql-release brew update brew install msodbcsql17 mssql-tools

Other OS see:

With ODBC installed, we can now connect to the DB with our credentials, query some data and save it in a Pandas DataFrame:

Read From Azure SQL Server DB and save in Pandas DataFrame

With your data in a Pandas df, all kinds of modeling and analysis can be done. When you are ready to save your results to the SQL database, follow the code below:

That’s all for my simple tutorial on querying data from and writing data back to Azure SQL Server databases from Python. I would love to hear what you think about this approach or your own experiences with Azure DB.

See you a next time!

--

--

Tina Bu
Tina Bu

Responses (1)