2017-01-17 32 views
1

Microsoft Azureデータウェアハウスに接続するために、pythonのsqlalchemyライブラリを使用しようとしています。 と、次のエラーを受信: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 

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() 

それとも

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() 

私は上記のコードを実行したとき、私は例外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',) 
関連する問題