我在Azure中有一个python webapp,我想从同一资源组中的Azure中的sqlServer读取.按照此示例Connecting to Microsoft SQL server using Python,我已将pyodbc添加到我的requirements.txt,部署到Azure失败,抱怨它没有可用的C可再发行组件(9.0)的正确版本.是否有任何可以做到这一点,或者是否需要不同的架构(如果是,那么?)?
解决方法:
我试图在我的烧瓶网络应用程序中访问Azure sql数据库.你可以参考我的工作代码.
view.py
from datetime import datetime
from flask import render_template
from jaygongflask import app
import pyodbc
@app.route('/database')
def database():
"""Renders the about page."""
cnxn = pyodbc.connect('DRIVER={ODBC Driver 13 for sql Server};SERVER=***.database.windows.net;DATABASE=***;UID=***;PWD=***')
cursor = cnxn.cursor()
cursor.execute("select * from dbo.Student")
row = cursor.fetchall()
#for r in row:
# print r
return render_template(
'database.html',
title='Database',
year=datetime.Now().year,
message='Database query result.',
queryResult = row
)
web.config中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Wsgi_HANDLER" value="jaygongflask.app"/>
<add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
<add key="Wsgi_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptprocessor="D:\home\python361x64\python.exe|D:\home\python361x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
</configuration>
安装pyodbc包
我的网络应用程序使用python361x64扩展.请参考我的步骤如下:
第1步:创建azure Web应用程序并添加扩展(这里是Python 3.6.1 x64)
web.config中:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="Wsgi_HANDLER" value="<Your Project Name>.app"/>
<add key="PYTHONPATH" value="D:\home\site\wwwroot"/>
<add key="Wsgi_LOG" value="D:\home\LogFiles\wfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptprocessor="D:\home\python361x64\python.exe|D:\home\python361x64\wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
</configuration>
步骤3:切换到Kudu CMD并命令cd python361x64并触摸get-pip.py并通过编辑将url https://bootstrap.pypa.io/get-pip.py的内容复制到get-pip.py中按钮,然后运行python get-pip.py来安装pip工具.
第4步:通过python -m pip install pyodbc安装pyodbc软件包或任何你需要的软件包
更多部署详情,请参阅此tutorial.
访问网址http://***.azurewebsites.net/database.
希望它可以帮助你.任何关注,请让我知道.