Azure SQL bindings for Functions - Python

Dyt13 211 Reputation points
2022-09-13T07:19:51.513+00:00

I'm trying to use Sql output binding functionalities for Azure function. (local debug). 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.

Azure SQL Database
Azure Functions
Azure Functions
An Azure service that provides an event-driven serverless compute platform.
5,909 questions
0 comments No comments
{count} votes

Accepted answer
  1. MuthuKumaranMurugaachari-MSFT 22,441 Reputation points Moderator
    2022-09-15T18:18:27.76+00:00

    @Dyt13 Thank you for reaching out to Microsoft Q&A. I noticed that you have added actual connection string in your code snippet, but this value refers to the name of app setting.

    "connectionStringSetting": "Driver={ODBC Driver 13 for SQL Server} etcetc."

    Refer Configuration docs which describes the following:

    241557-image.png

    Add SQL connection string in your app settings as described in docs: Local settings file and set connectionStringSetting value with the name of the setting (SQLConnectionString in the sample). You can find full code samples in GitHub repository.

    I hope this answers your question and feel free to add if you have any questions. I would be happy to assist you.

    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community.

    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.