![](/img/trans.png)
[英]How to connect to Azure sql database with python SQL alchemy using Active directory integrated authentication
[英]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.