本文介绍了如何使用python访问local.setting.json数据库连接字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在本地Azure函数(Http触发)中有此local.settings.json

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 "
  }
}

我想访问变量"SQLConnectionString"并在我的Python3.6代码中使用.我找到了许多使用C#访问此变量的指南,但我很不幸运地找到如何通过python进行操作.

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.

推荐答案

我刚刚对其进行了测试,并遇到了相同的错误.您应该按如下所示修改local.setting.json:

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\", .....)"
  }
}

但不是

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

请尝试一下.

作为补充〜(我想提供解决方案"os.environ [xxx]",但看到了HariHaran提供的解决方案,哈哈〜)

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

这篇关于如何使用python访问local.setting.json数据库连接字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 14:42