我正在努力;
- 当 Cosmos DB 收到数据时,运行 Cosmos DB 触发的 Python 代码。
- Azure Functions 中的 Python 代码包含从 Azure MySQL 提取数据的代码。
我所做的是;
- 。在Azure Functions中编写python并通过Cosmos触发运行它 D B。这次成功了。
- 。安装mysql.connector引用 https://prmadi.com/running-python-code-on-azure-functions-app/和 运行代码连接到Azure MySQL,但它不起作用。
您知道如何将Python的mysql模块安装到Azure Functions并连接到数据库吗?
谢谢!
最佳答案
根据您的描述,我认为您的问题是如何在Azure函数应用中安装Python第三方模块。
请引用以下步骤:
第 1 步:
登录库杜:https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole
.
在 d:/home/site/wwwroot/<your function name>
中运行以下命令文件夹。(需要一些时间)
python -m virtualenv myvenv
第 2 步:
通过以下命令加载环境 env/Scripts
文件夹。
activate.bat
第 3 步:
您的 shell 现在应该以 (env) 为前缀。
更新点
python -m pip install -U pip
安装你需要的东西
python -m pip install MySQLdb
第 4 步:
在您的代码中,更新 sys.path 以添加此 venv:
import sys, os.path
sys.path.append(os.path.abspath(os.path.join(os.path.dirname( __file__ ), 'env/Lib/site-packages')))
然后通过下面的代码片段连接到 mysql 数据库
#!/usr/bin/python
import MySQLdb
# Connect
db = MySQLdb.connect(host="localhost",
user="appuser",
passwd="",
db="onco")
cursor = db.cursor()
# Execute SQL select statement
cursor.execute("SELECT * FROM location")
# Commit your changes if writing
# In this case, we are only reading data
# db.commit()
# Get the number of rows in the resultset
numrows = cursor.rowcount
# Get and display one row at a time
for x in range(0, numrows):
row = cursor.fetchone()
print row[0], "-->", row[1]
# Close the connection
db.close()
希望对您有帮助。
关于python - 如何通过 Python 从 Azure Functions 连接到 Azure MySQL,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47360783/