简体   繁体   中英

What is the name of the driver to connect to Azure SQL Database from pyodbc in Azure ML?

I'm trying to create a ' Reader ' alternative to read data from Azure SQL Database using the 'Execute python script' module in Azure ML . while doing so, I'm trying to connect to Azure Sql using pyodbc library. here's my code:

def azureml_main(dataframe1 = None, dataframe2 = None):
    import pyodbc   
    import pandas as pd

    conn = pyodbc.connect('DRIVER={SQL Server}; SERVER=server.database.windows.net; DATABASE=db_name; UID=user; PWD=Password')
    SQLCommand = ('''select * from table1 ''')
    data_frame = pd.read_sql(SQLCommand, conn)
    return data_frame,

also tried to use a different driver name: {SQL Server Native Client 11.0}

Here is the error i'm getting:

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

Does anybody know which driver should I use?

just to make sure, I tried "{SQL Server}", "{SQL Server Native Client 11.0}" and "{SQL Server Native Client 10.0}" and got the same error

I also tried a different format:

conn = pyodbc.connect('DRIVER={SQL Server}; SERVER=server.database.windows.net; DATABASE=db_name; user=user@server; password=Password')

and

conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0}; SERVER=server.database.windows.net; DATABASE=db_name; user=user@server; password=Password')

According to this answer , the connection string should be:

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

Note the difference in the format: different params for user, password and database vs. all in one in the first string.

Also related, see this Azure page: Connect to SQL Database by using Python on Windows . It states using pymssql , with no mention of pyodbc .

you will need the Microsoft ODBC Driver to use pyodbc. You can download it from here: https://www.microsoft.com/en-us/download/details.aspx?id=36434 . Once you download that, try using the following connection string:

conn = pyodbc.connect('DRIVER={SQL Server Native Client 11.0}; SERVER=server.database.windows.net; DATABASE=db_name; user=user@server; password=Password')

If this string does no work, try this one:

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

If you still can't use pyodbc let me know.

Cheers,
Meet

I got an answer from azure support:

Currently it is not possible to access sql azure dbs from within an “execute python script” module. As you suspected this is due to missing odbc drivers in the execution environment. Suggested workarounds are to a) use reader module or b) export to blobs and use the Azure Python SDK for accessing those blobs http://blogs.msdn.com/b/bigdatasupport/archive/2015/10/02/using-azure-sdk-for-python.aspx

So currently it it impossible to connect to SQL server from “execute python script” module in Azure-ML. If you like to change it, please vote here

The ultimate source of truth :-) for connection strings is:

https://www.connectionstrings.com/sql-azure/

Use it as guide to construct yours.

The SQL Database Driver name for Pyodbc should be {SQL Server Native Client 10.0} since some reasons below.

  1. The connection string for SQL Database shown on Azure portal, please see the Fig 1 & Fig 2 below.

Fig 1. On Azure old portal 在此输入图像描述

Fig 2. On Azure new portal (the version 11.0 later then v10.0) 在此输入图像描述

  1. According to the Pyodbc code comments , please see the Fig 3 below.

Fig 3. 在此输入图像描述

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM