【问题标题】:How to connect to the sql azure database with python SQL alchemy using active directory integrated authentication如何使用活动目录集成身份验证通过 python SQL alchemy 连接到 sql azure 数据库
【发布时间】:2021-07-13 08:39:57
【问题描述】:

我正在使用如下的连接字符串

params=parse.quote_plus("Driver={ODBC Driver 17 For SQL server};Server=tcp:server name,1433;database=database name;Encrypt=yes;TrustServerCertificate=no;Authentication=ActiveDirectoryIntegrated'
engine=sqlalchemy.create_engine("mssql:///?odbc_connect=%s" %params)

使用上面的连接字符串它给了我错误

[Microsoft][ODBC Driver 17 for SQL server][SQL server]111214 an attempt to an attempt to complete the transaction has failed no corresponding transaction found 

【问题讨论】:

  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…
  • 嗨,Jason,感谢您的回答,但当我通过活动目录从 SSMS 登录时,我确实知道该服务器的密码 - 集成身份验证,所以请您帮我解决为什么我得到没有找到交易的错误
  • 请尝试我更新的答案。
  • 嗨,杰森,感谢它对我有用的答案
  • 如果我的回复有帮助,请采纳为答案(点击回复旁边的标记选项将其从灰色切换为填写。),请参阅meta.stackexchange.com/questions/5234/…

标签: python sqlalchemy azure-active-directory ssms


【解决方案1】:

更新

你可以加connect_args,然后试试。

请确保您有相同的帐户登录您的 windows pc 和 sql server。

engine = create_engine('mssql+pyodbc:///?odbc_connect=%s' % params, echo=True, connect_args={'autocommit': True})

以前的

您可以考虑使用Authentication=ActiveDirectoryPassword,它比Authentication=ActiveDirectoryIntegrated 更简单,下面的代码适合我。

感谢Peter Pan的回答,更多细节可以参考他的描述。他的回答在他的描述中有Authentication=ActiveDirectoryIntegrated的详细用法,我更喜欢Authentication=ActiveDirectoryPassword,所以我把我的回答贴出来了,你可以参考一下。

How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication

from urllib import parse
from sqlalchemy import create_engine

your_user_name = 'pa**i@**a.onmicrosoft.com'
your_password_here = 'J***20'
connecting_string = 'Driver={ODBC Driver 17 for SQL Server};Server=tcp:yoursqlserver.database.windows.net,1433;Database=yoursqldb;Uid='+your_user_name+';Pwd='+your_password_here+';Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;Authentication=ActiveDirectoryPassword'
params = parse.quote_plus(connecting_string)

engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)
connection = engine.connect()
result = connection.execute("select 1+1 as res")
for row in result:
    print("res:", row['res'])
connection.close()

【讨论】:

    猜你喜欢
    • 2019-11-16
    • 2021-09-25
    • 2018-09-21
    • 2018-01-17
    • 2013-05-07
    • 1970-01-01
    • 2018-10-01
    • 1970-01-01
    • 2020-04-13
    相关资源
    最近更新 更多