2017-01-17 3 views
1

pyure의 sqlalchemy 라이브러리를 사용하여 Microsoft Azure 데이터웨어 하우스에 연결하려고합니다. 다음과 같은 오류가 발생 :Microsoft Azure 데이터웨어 하우스 및 SqlAlchemy

(pyodbc.Error) ('HY000', '[HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Client driver version is not supported. (46722) (SQLDriverConnect); [HY000] [Microsoft][ODBC SQL Server Driver][SQL Server]Client driver version is not supported. (46722)') 

창 연결을위한 내 코드 : 사전에

import sqlalchemy 
user_name = 'userName' 
password = 'password' 
uri = 'sqlServerName' 
db_name = 'SQLDBName' 
db_prefix = 'mssql+pyodbc://' 
db_driver = '{SQL Server}' 
connection_string = "{db_prefix}{user_name}:{password}@{uri}/{db_name}?Driver={driver}".format(
       db_prefix=db_prefix, user_name=user_name, password=password, uri=uri, db_name=db_name, 
       driver=db_driver) 
engine = sqlalchemy.engine.create_engine(connection_string, echo=echo, pool_size=20, 
                  max_overflow=100) 
engine.connect() # throws the error 

감사합니다!

답변

3

코드에 따르면이 문제는 잘못된 Driver={SQL Server}을 사용하여 발생한 것으로 보입니다.

Azure 포털에서 아래 그림과 같이 연결 문자열을 얻을 수 있습니다.

enter image description here

올바른 ODBC 드라이버 이름은 Driver={ODBC Driver 13 for SQL Server}해야한다. 한편 tutorial에 따라 3.1.1pyodbc을 현재 환경에 맞게 설치하십시오.

다음은 내 테스트 코드입니다. 나는 위의 코드를 실행할 때

import sqlalchemy 
import urllib 

params = urllib.quote_plus("Driver={ODBC Driver 13 for SQL Server};Server=<server-host>.database.windows.net,1433;Database=<database>;Uid=<user>@<server-host>;Pwd=<password>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;") 
engine = sqlalchemy.engine.create_engine("mssql+pyodbc:///?odbc_connect=%s" % params) 
engine.connect() 

import sqlalchemy 

connection_string = "mssql+pyodbc://<user>@<server-host>:<password>@<server-host>.database.windows.net:1433/<database>?driver=ODBC+Driver+13+for+SQL+Server" 
engine = sqlalchemy.engine.create_engine(connection_string) 
engine.connect() 

또는

나는 예외 sqlalchemy.exc.ProgrammingError: (pyodbc.ProgrammingError) ('42000', "[42000] [Microsoft][ODBC Driver 13 for SQL Server][SQL Server]Catalog view 'dm_exec_sessions' is not supported in this version. (104385) (SQLExecDirectW)")를 얻었으나,이 작업에 영향을주지 것 같다.

그리고 아래 코드를 pyodbc으로 만 테스트하면 완벽하게 작동합니다.

import pyodbc 
cnxn = pyodbc.connect("Driver={ODBC Driver 13 for SQL Server};Server=<server-host>.database.windows.net,1433;Database=<database>;Uid=<user>@<server-host>;Pwd=<password>;Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;") 
cursor = cnxn.cursor() 
cursor.execute("select @@VERSION") 
row = cursor.fetchone() 
if row: 
    print row 

출력 :

(u'Microsoft Azure SQL Data Warehouse - 10.0.8529.1 Jan 13 2017 22:49:03 Copyright (c) Microsoft Corporation',) 
관련 문제