Getting started with pyodbc and Cloud SQL for SQL Server

Doug Mahugh
1 min readDec 16, 2019

If you’re a Python developer or data scientist who wants to use pyodbc to access a Cloud SQL for SQL Server database instance, here’s a Jupyter notebook to help you get started. The notebook starts with how to build an ODBC connection string and connect to a SQL Server instance via the Cloud SQL proxy, then goes through creating a database and a table, adding some data from a public data set, running a query, and putting query results in a Pandas dataframe.

Pyodbc is useful for quickly creating administrative tools and ETL scripts or integrating SQL data sources into notebooks for data analysis and modeling. As an implementation of PEP 249 (Python Database API Specification v2.0), it provides a simple interface for connecting to a database and executing SQL commands.

The notebook and setup instructions are available in this GitHub repo: dmahugh/cloud-sql-pyodbc. Follow the instructions there to run it locally in Jupyter, VS Code, or Azure Data Studio, where you can connect to your own database instance for an interactive learning experience. Alternatively, you can skip the setup steps and simply open the notebook in your browser to review the snippets and their output.

--

--