在python中导入mysql.connector在timer trigger azure函数中无法工作

zzoitvuj  于 12个月前  发布在  Python
关注(0)|答案(1)|浏览(103)

我试图使用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.')

字符串

92vpleto

92vpleto1#

我在代码中的导入中添加了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.')

字符串

requirements.txt:

azure-functions
mysql-connector-python

输出:

运行成功并触发定时器触发功能,如下所示。


的数据

相关问题