skip to Main Content

I am developing a azure function on my local with python v2. I am trying to connect my local function to a local mysql db, but I can’t because I don’t have function.json file.

V2 model does not use funtion.json file. Where Shall I setup that data then?

When I read doc https://learn.microsoft.com/en-us/azure/azure-functions/functions-bindings-azure-sql-input?tabs=isolated-process%2Cnodejs-v4&pivots=programming-language-python#http-trigger-get-row-by-id-from-query-string-5 , which I am trying to follow, It tells you to setup some things in function.json file. However, that file is only in python v1 programming model, but not in v2.

2

Answers


  1. Chosen as BEST ANSWER

    Thanks @Vivek Vaibhav Shandilya for clarifying "Yes, Model V2 does not have function.json. bindings are defined inside the function_app.py file."

    So simply solved it using mysql-connector-python (https://pypi.org/project/mysql-connector-python/) in my function_app.py file. Here is an example of what I have added:

    import mysql.connector
    
    def execute_query(query):
        connection = mysql.connector.connect(
            host="localhost",
            user="my_user",
            password="password",
            database="my_database"
        )
        cursor = connection.cursor()
        cursor.execute(query)
        result = cursor.fetchall()
        cursor.close()
        connection.close()
        return result
    

  2. Currently Python programming model v2 does not support SQL Trigger ,SQL Input , SQL Output.
    When you create function using func command python model V2 is default selected.
    and there is no option to select SQL trigger, SQL Input, SQL Output in model V2.

    PS C:UsersxxxxxDesktopVS code FoldersPython> func init --worker-runtime python
    Found Python version 3.11.7 (py).
    The new Python programming model is generally available. Learn more at https://aka.ms/pythonprogrammingmodel
    Writing requirements.txt
    Writing function_app.py
    Writing .gitignore
    Writing host.json
    Writing local.settings.json
    Writing C:UsersxxxxxDesktopVS code FoldersPython.vscodeextensions.json
    

    enter image description here

    PS C:UsersxxxxxDesktopVS code FoldersPython> func new
    Use the up/down arrow keys to select a template:
    Blob trigger
    CosmosDB trigger
    EventGrid trigger
    EventHub trigger
    HTTP trigger
    Queue trigger
    ServiceBus Queue trigger
    ServiceBus Topic trigger
    Timer Trigger
    

    enter image description here

    If you want to create in V1 model you need to mention model v1 in the command.

    bash command:

    func init --worker-runtime python --model v1   
    
    PS C:UsersxxxxxDesktopVS code FoldersPython> func init --worker-runtime python --model v1
    Found Python version 3.11.7 (py).
    
    Writing requirements.txt
    Writing getting_started.md
    Writing .gitignore
    Writing host.json
    Writing local.settings.json
    Writing C:UsersxxxxxDesktopVS code FoldersPython.vscodeextensions.json
    

    enter image description here

    PS C:UsersxxxxxDesktopVS code FoldersPython> func new
    Use the up/down arrow keys to select a template:
    Azure Blob Storage trigger
    Azure Cosmos DB trigger
    Durable Functions activity
    Durable Functions entity
    Durable Functions HTTP starter
    Durable Functions orchestrator
    Azure Event Grid trigger
    Azure Event Hub trigger
    HTTP trigger
    Kafka output
    Kafka trigger
    Azure Queue Storage trigger
    RabbitMQ trigger
    Azure Service Bus Queue trigger
    Azure Service Bus Topic trigger
    SQL Input Binding
    SQL Output Binding
    SQL Trigger
    Timer trigger
    
    

    enter image description here

    PS C:UsersxxxxxDesktopVS code FoldersPython> func new
    Use the up/down arrow keys to select a template:SQL Trigger
    Function name: [SqlTriggerBinding] sql_trigger
    Writing C:UsersxxxxxDesktopVS code FoldersPythonsql_triggerreadme.md
    Writing C:UsersxxxxxDesktopVS code FoldersPythonsql_trigger__init__.py
    Writing C:UsersxxxxxDesktopVS code FoldersPythonsql_triggerfunction.json
    The function "sql_trigger" was created successfully from the "SQL Trigger" template.
    

    enter image description here

    Result :

    new function will be created with function.json file

    enter image description here

    function.json:

    {
      "bindings": [
        {
          "name": "changes",
          "type": "sqlTrigger",
          "direction": "in",
          "tableName": "[dbo].[table1]",
          "connectionStringSetting": "SqlConnectionString"
        }
      ],
      "disabled": false
    }
    

    In VS Code :

    VS Code:

    enter image description here

    after selecting the programming language, option will be given to select model.

    enter image description here

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