skip to Main Content

I’m trying to use Sql output binding functionalities for Azure function.
Below is my current configuration. I’m using Visual Studio Code as IDE

Found Python version 3.9.0 (py).
Core Tools Version: 4.0.4736 Commit hash: N/A (64-bit)
Function Runtime Version: 4.8.1.18957
Extension bundle version: "[4.*, 5.0.0)"

functions.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"
    )

Error :

[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.

Can you please help on that.

2

Answers


  1. Value for ConnectionString must be included in local.settings.json (not env variable).

    Login or Signup to reply.
  2. Check your binding declaration in function.json it should look like this:

    {
        "name": "todoItems",
        "type": "sql",
        "direction": "out",
        "commandText": "dbo.ToDo",
        "connectionStringSetting": "SqlConnectionString"
    }
    

    and the ConnectionString in your local.settings.json

    "SqlConnectionString": "Server={Azure SQL Server};Initial Catalog={Database name};Persist Security Info=False;User ID={user};Password={password};"
    

    Here is a full sample for input and output bindings:
    https://github.com/DFMERA/azure-functions-sql-binding

    Blog Post:
    https://acelera.tech/2023/02/10/azure-functions-sql-binding-con-python/

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search