python Azure函数中的SQL绑定

wgmfuz8q  于 2023-02-21  发布在  Python
关注(0)|答案(2)|浏览(156)

我正在尝试对Azure函数使用Sql输出绑定功能。以下是我的当前配置。我正在使用Visual Studio代码作为IDE

    • 找到Python版本3.9.0(py)。

核心工具版本:4.0.4736提交哈希:不适用(64位)
函数运行时版本:4.8.1.18957
扩展包版本:"[第4. * 条、第5.0.0条)"**
函数. json:

{
  "scriptFile": "__init__.py",
  "bindings": [
    {
      "authLevel": "anonymous",
      "type": "httpTrigger",
      "direction": "in",
      "name": "req",
      "methods": [
        "post"
      ]
    },
    {
      "type": "http",
      "direction": "out",
      "name": "$return"
    },
    {
      "name": "transactions",
      "type": "sql",
      "direction": "out",
      "commandText": "dbo.[table]",
      "connectionStringSetting": "Driver={ODBC Driver 13 for SQL Server} etcetc."
    }
  ]
}

init.py

import logging
import json

import azure.functions as func

def main(
    req: func.HttpRequest, transactions: func.Out[func.SqlRow]
) -> func.HttpResponse:
    logging.info("Python HTTP trigger function processed a request.")

    # i'm using Vue JSON.Stringify({}) in the POST method...on client side
    body = json.loads(req.get_body())
    row = func.SqlRow.from_dict(body)
    transactions.set(row)

    return func.HttpResponse(
        body=json.dumps(body), status_code=201, mimetype="application/json"
    )

错误:

[2022-09-12T17:49:13.731Z] Executed 'Functions.RegisterNewBatch' (Failed, Id=..., Duration=9ms)
[2022-09-12T17:49:13.732Z] System.Private.CoreLib: Exception while executing function:  
 Functions.RegisterNewBatch. Microsoft.Azure.WebJobs.Host: 
Error while handling parameter _binder after function returned:  
 Microsoft.Data.SqlClient: The ConnectionString property has not been initialized.

你能帮忙吗。

kgsdhlau

kgsdhlau1#

ConnectionString的值必须包含在local.settings.json(不是环境变量)中。

pftdvrlh

pftdvrlh2#

检查function.json中的绑定声明和local.settings.json中的ConnectionString
下面是输入和输出绑定的完整示例:https://github.com/DFMERA/azure-functions-sql-binding
博客文章:https://acelera.tech/2023/02/10/azure-functions-sql-binding-con-python/

相关问题