簡體   English   中英

如何通過Python從Azure Functions連接到Azure MySQL

[英]How to connect to Azure MySQL from Azure Functions by Python

我在嘗試着;

  1. 當cosmos DB收到數據時,運行由Cosmos DB觸發的python代碼。
  2. Azure Functions中的python代碼具有從Azure MySQL提取數據的代碼。

我所做的是

您知道如何將Python的mysql模塊安裝到Azure Functions並連接到數據庫嗎?

謝謝!

根據您的描述,我認為您的問題與如何在Azure函數應用程序中安裝Python第三方模塊有關。

請參考以下步驟:

第1步 :

登錄kudu: https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole : https://Your_APP_NAME.scm.azurewebsites.net/DebugConsole

d:/home/site/wwwroot/<your function name>文件夾中運行以下命令(需要一些時間)

python -m virtualenv myvenv

第2步 :

通過以下命令在env/Scripts文件夾中加載env。

activate.bat

第三步:

您的外殼現在應該以(env)為前綴。

更新點

python -m pip install -U pip

安裝您需要的

python -m pip install MySQLdb

第四步 :

在您的代碼中,更新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 db

#!/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()

希望對您有幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM