Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access local.setting.json db connection string with python

I have this local.settings.json in my local Azure function (Http triggered)

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  },
  "ConnectionStrings": {
    "SQLConnectionString": "valid connection string "
  }
}

I want to access variable "SQLConnectionString" and use in my Python3.6 code. I have found many guides for accessing this variable with C#, but I wasn't lucky to find how to do it via python.

like image 923
Michal Štěpán Avatar asked Oct 15 '25 07:10

Michal Štěpán


1 Answers

I test it just now and met the same error. You should modify your local.setting.json as below:

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python",
    "SQLConnectionString": "cnx = mysql.connector.connect(user=\"test@hurytestmysqlserver\", password=\"Password123\", host=\"testmysqlserver.mysql.database.azure.com\", port=3306, database=\"xxx\", .....)"
  }
}

but not

{
  "IsEncrypted": false,
  "Values": {
    "AzureWebJobsStorage": "",
    "FUNCTIONS_WORKER_RUNTIME": "python"
  },
  "ConnectionStrings": {
    "SQLConnectionString": "xxxxxxxx"
  }
}

Please have a try.

Just as a supplement~(I wanted to provide the solution "os.environ[xxx]" but saw the solution provided by HariHaran, haha~)

like image 194
Hury Shen Avatar answered Oct 17 '25 01:10

Hury Shen