我试图使用 MySQL 数据库通过 Azure 函数中的计时器触发器在 Python 中生成定期摘要报告。 Python 代码使用 import mysql.connector 来让我的查询数据自行正常工作。但是,当我将这部分代码插入到触发器操作中时,我得到了 ModuleNotFoundError: No module named 'mysql'。
MySQL 连接器和计时器触发功能生成的电子邮件报告都运行良好,但将它们放在一起时不起作用。
import logging
import azure.functions as func
app = func.FunctionApp()
@app.schedule(schedule="*/30 * * * * *", arg_name="myTimer",
run_on_startup=True,
use_monitor=False)
def timer_trigger1(myTimer: func.TimerRequest) -> None:
import mysql.connector
bp1db = mysql.connector.connect(
host="xxxx.xxxxx.com",
user="xxxxx",
password="xxxx01",
db = 'boppwh'
)
cur1 = bp1db.cursor()
cur1.execute("SELECT COUNT(cellid) AS free FROM cells WHERE Status = 0")
result1 = cur1.fetchall()
free1 = result1[0]
logging.info('Python timer trigger function executed.')
我将 import mysql.connector 添加到代码中的导入中,并且能够触发计时器触发功能。
代码:
import logging
import azure.functions as func
import mysql.connector
app = func.FunctionApp()
@app.schedule(schedule="*/30 * * * * *", arg_name="myTimer", run_on_startup=True,
use_monitor=False)
def timer_trigger1(myTimer: func.TimerRequest) -> None:
bp1db = mysql.connector.connect(
host="<server_name>.mysql.database.azure.com",
user="<user_name>",
password="<password>",
db='boppwh'
)
cur1 = bp1db.cursor()
cur1.execute("SELECT COUNT(cellid) AS free FROM cells WHERE Status = 0")
result1 = cur1.fetchall()
free1 = result1[0]
logging.info('Python timer trigger function executed.')
需求.txt:
azure-functions
mysql-connector-python
输出:
运行成功并触发如下定时器触发函数。