Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

connect to Azure SQL database via pyodbc

I use pyodbc to connect to my local SQL database which works withoout problems.

SQLSERVERLOCAL='Driver={SQL Server Native Client 11.0};Server=(localdb)\\v11.0;integrated security = true;DATABASE=eodba;'
cnxn = pyodbc.connect(SQLSERVERLOCAL)   #works

I try the connection to the azure sql database with:

SQLSERVERAZURE='Driver={SQL Server Native Client 10.0};Server=tcp:mydatbase.database.windows.net,1433;Database=mydb;Uid=myuser@myerver;Pwd=mypass;Encrypt=yes;Connection Timeout=30;'
cnxn = pyodbc.connect(SQLSERVERAZURE)   #works not

what gives me the error:

[Microsoft][ODBC Driver Manager] Data source name not found and no default driver specified (0) (SQLDriverConnect)')
like image 957
daniel Avatar asked Jun 12 '15 11:06

daniel


People also ask

How do I connect to Azure SQL Database from Visual Studio code?

In Visual Studio Code, press Ctrl+Shift+P (or F1) to open the Command Palette. Select MS SQL:Connect and choose Enter. Select Create Connection Profile. Follow the prompts to specify the new profile's connection properties.


1 Answers

Besides the suggestions that provided by meet-bhagdev who recommended to use pymssql dirve that mentioned in link, to resolve the error: Data source name not found and no default driver specified (0) (SQLDriverConnect)') that encountered, please update your connect string as below to see if it works.

conn = pyodbc.connect('DRIVER={SQL Server};SERVER=yoursqlAzureServer.database.windows.net,1433', user='yourName@yoursqlAzureServer', password='Password', database='DBName')

like image 90
Ming Xu - MSFT Avatar answered Sep 28 '22 08:09

Ming Xu - MSFT