Skip to content
Advertisement

How to connect to a Azure SQL Server through Python

I’ve come across some trouble accessing my SQL Server through Python. I can get it going when my SQL Server is installed locally, but not when it is online.

I have used the following code:

Connection to SQL Server

but then when I try and run:

I get the error:

InterfaceError: (‘IM002’, ‘[IM002] [Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)’)

I can see there is something to do with the driver however I access both a local SQL Server DB (which the above code works for) and the Azure SQL.

Any help would be greatly appreciated! Thanks

Advertisement

Answer

Have you tried pyodbc that way?

/.env/db.conf file with connection details:

Connection and retrieving data:

Also please refer to this Docs.MS page.

EDIT#1:

I have tried using pandas DataFrame and sqlalchemy:

Output was:

I checked Azure portal, table Users was created and has 3 rows.

enter image description here

I’ve tried:

Output:

Azure portal:

enter image description here

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement