【问题标题】:Microsoft Azure Data warehouse and SqlAlchemyMicrosoft Azure 数据仓库和 SqlAlchemy
【发布时间】:2017-06-01 19:09:34
【问题描述】:

我正在尝试使用 python 的 sqlalchemy 库连接到 microsoft azure 数据仓库。 并收到以下错误:

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

我的 windows 连接代码:

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

提前致谢!

【问题讨论】:

    标签: python python-2.7 azure sqlalchemy data-warehouse


    【解决方案1】:

    根据您的代码,问题似乎是由于使用了不正确的Driver={SQL Server}。

    在 Azure 门户上,您可以按照下图的步骤获取连接字符串。

    正确的 odbc 驱动程序名称应为 Driver={ODBC Driver 13 for SQL Server}。同时,请按照tutorial为您当前环境安装pyodbc的正确版本3.1.1。

    这是我的测试代码如下。

    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', )
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-12
      • 1970-01-01
      • 2016-06-30
      • 2019-05-28
      • 2017-01-04
      • 2016-10-26
      • 1970-01-01
      • 2016-05-21
      相关资源
      最近更新 更多