Azure Functions Python 连接到 Azure SQL 数据库

Azure Functions Python connect to Azure SQL DB

我使用 Python 创建了一个 Azure Functions 并想将一些数据写入 Azure SQL 数据库。
如果我 运行 通过 AZ Function Debugger 在本地机器上运行代码,一切正常。但是,当我将所有内容部署到 Azure 时,我只收到一条消息,指出存在错误(没有其他具体信息)。
我认为这与 ODBC 驱动程序有关? 我正在使用以下代码连接和插入数据:

with pyodbc.connect('DRIVER='+driver+';SERVER=tcp:'+server+';PORT='+port+';DATABASE='+database+';UID='+username+';PWD='+ password + ";Authentication=ActiveDirectoryPassword", timeout=120) as conn:
        with conn.cursor() as cursor:
            try:
                cursor.execute(data)
            except:
                logging.error("Can't execute SQL Query!")             

我使用driver= '{ODBC Driver 17 for SQL Server}'作为驱动程序。
我假设这在 Azure 中缺失?如何解决这个问题?通过 Python 从 Azure Functions 连接到 Azure SQL 数据库的正确方法是什么?

似乎包含了 ODBC 驱动程序,只是文档不足:

https://github.com/MicrosoftDocs/azure-docs/issues/54423

这里有一个示例项目:

https://github.com/kevin808/azure-function-pyodbc-MI

可在此处找到包括创建系统分配身份在内的完整教程:

https://techcommunity.microsoft.com/t5/apps-on-azure-blog/how-to-connect-azure-sql-database-from-python-function-app-using/ba-p/3035595

目前有一个 SQL 扩展正在开发中,但它目前仅支持 C#。 Python 已被请求作为改进,因此您可以将您的问题添加到问题中,以便您可以使用绑定

https://github.com/Azure/azure-functions-sql-extension/issues/172