在Azure上的Pyodbc [英] pyodbc on Azure
问题描述
我在Azure中有一个python webapp,我也想在同一资源组中的Azure中从SQLServer读取.按照此示例使用Python连接到Microsoft SQL Server ,我添加了pyodbc到我的requirements.txt,到Azure的部署失败,抱怨它没有可用的C ++可再发行版本(9.0)的正确版本.
I have a python webapp in Azure which I would like to read from an SQLServer also in Azure in the same resource group. Following this example Connecting to Microsoft SQL server using Python, I have added pyodbc to my requirements.txt, the deployment to Azure fails complaining that it doesn't have the correct version of C++ redistributable (9.0) available. Is there anything that can be done about this, or is a different architecture required (and if so, which?)?
推荐答案
我试图在我的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
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:
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软件包
Install pyodbc package
我的网络应用程序可使用python361x64扩展名.请参考我执行的步骤,如下所示:
My web app works with python361x64 extension. Please refer to the steps I did as below:
第1步:创建azure网络应用并添加扩展程序(此处为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:\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
和touch get-pip.py
,然后通过编辑"按钮将URL https://bootstrap.pypa.io/get-pip.py
的内容复制到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安装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.
获取查询结果
Get query result
访问网址http://***.azurewebsites.net/database
.
希望对您有所帮助.有任何问题,请告诉我.
Hope it helps you.Any concern, please let me know.
这篇关于在Azure上的Pyodbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!