繁体   English   中英

如何连接到 sql azure 数据库与 python Z9778840A0100CB30C982876741B0B5yA 使用集成的身份验证

[英]How to connect to the sql azure database with python SQL alchemy using active directory integrated authentication

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

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 

更新

您可以添加connect_args ,然后尝试。

请确保您有相同的帐户登录您的 windows 电脑和 sql 服务器。

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

以前的

您可以考虑使用比 Authentication= Authentication=ActiveDirectoryPassword更容易的Authentication=ActiveDirectoryIntegrated ,下面的代码对我有用。

谢彼得潘的回答,更多细节可以参考他的描述。 他的答案在他的描述中有详细的Authentication=ActiveDirectoryIntegrated用法,我更喜欢Authentication=ActiveDirectoryPassword ,所以我发布了我的答案,你可以参考它。

如何使用 python Z9778840A0100CB30C982876741B0B5yA 连接到 Azure sql 数据库

在此处输入图像描述

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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