将 odbc 驱动程序安装到 azure 应用服务 [英] install odbc driver to azure app service
问题描述
我在需要访问 Azure SQL 数据库的 Azure 应用服务中运行一个简单的 Python Web 应用.为了从 python 查询 Azure SQL 数据库,需要安装 ODBC 驱动程序.我已经在我的本地机器上完成了这项工作,并且运行良好.如何在运行我的应用的 Azure 计算机上安装 ODBC 驱动程序,或者是否有必要?
I am running a simple Python web app in the Azure app service that needs to acces an Azure SQL database. In order to que an Azure SQL database from python one needs to install the ODBC driver. I have done that at my local machine and it works perfectly. How do I install the ODBC driver at the Azure machine that is running my app or if it is nescessary at all?
推荐答案
根据我的经验,Azure Web 应用服务运行时是 Windows 系统.这不需要重新加载驱动程序.
Per my experience , the Azure Web App Service Runtime is Windows system. That does not require a reload driver.
我尝试在我的 Flask Web 应用中访问 Azure SQL 数据库.
I tried to access Azure SQL Database in my flask web app.
你可以参考我的工作代码.
You could refer to my working code.
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
)
安装pyodbc包
这里,我使用 python361x64 扩展.所以我在 KUDU 中运行命令 python -m pip install pyodbc
.
Here, I use python361x64 extension. So I run the command python -m pip install pyodbc
in KUDU.
获取查询结果
访问 url http://***.azurewebsites.net/database
.
希望对您有所帮助.有任何问题,请告诉我.
Hope it helps you.Any concern, please let me know.
更新答案:
web.config:
web.config:
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<appSettings>
<add key="WSGI_HANDLER" value="jaygongflask.app"/>
<add key="PYTHONPATH" value="D:homesitewwwroot"/>
<add key="WSGI_LOG" value="D:homeLogFileswfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:homePython361x64python.exe|D:homePython361x64wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
</configuration>
<小时>
更新答案 2:
Update Answer 2:
我的网络应用程序使用 python361x64 扩展.请参考我做的步骤如下:
My web app works with python361x64 extension. Please refer to the steps I did as below:
第 1 步:创建 azure web 应用程序并添加扩展(这里是 Python 3.6.1 x64)
Step 1 : Create azure web app and add Extensions(here is Python 3.6.1 x64)
第 2 步:发布您的 flask
项目并添加 web.config
.
Step 2 : Publish your flask
project and add the web.config
.
web.config:
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:homesitewwwroot"/>
<add key="WSGI_LOG" value="D:homeLogFileswfastcgi.log"/>
</appSettings>
<system.webServer>
<handlers>
<add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:homePython361x64python.exe|D:homePython361x64wfastcgi.py" resourceType="Unspecified" requireAccess="Script"/>
</handlers>
</system.webServer>
</configuration>
第三步:切换到Kudu CMD命令cd Python361x64
和touch get-pip.py
,复制url内容https://bootstrap.pypa.io/get-pip.py
通过Edit按钮进入get-pip.py
,然后运行python get-pip.py
进行安装pip 工具.
Step 3: Switch to the Kudu CMD and commands cd Python361x64
and touch get-pip.py
and copy the content of the url https://bootstrap.pypa.io/get-pip.py
into the get-pip.py
via Edit button, then run python get-pip.py
to install the pip tool.
第 4 步:通过 python -m pip install pyodbc 安装 pyodbc 包或任何你需要的包
Step 4 : Install pyodbc package or any packages you need via python -m pip install pyodbc
更多部署细节,请参考这个教程一>.
More deployment details , please refer to this tutorial.
这篇关于将 odbc 驱动程序安装到 azure 应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!