本文介绍了安装odbc驱动程序以使用Azure应用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Azure应用服务中运行一个简单的Python Web应用,该应用需要访问Azure SQL数据库.为了从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 App Service运行时是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

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软件包

Install pyodbc package

在这里,我使用python361x64扩展名.所以我在KUDU中运行命令python -m pip install pyodbc.

Here, I use python361x64 extension. So I run the command python -m pip install pyodbc in KUDU.

获取查询结果

Get query result

访问网址http://***.azurewebsites.net/database.

希望对您有帮助.有任何问题,请告诉我.

Hope it helps you.Any concern, please let me know.

更新答案:

Update Answer :

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>


更新答案2:


Update Answer 2:

我的网络应用程序可使用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 Python361x64touch 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.

这篇关于安装odbc驱动程序以使用Azure应用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-01 19:39