Azure上的pyodbc

问题描述 投票:2回答:1

我在Azure中有一个python webapp,我想从同一资源组中的Azure中的SQLServer读取。在这个示例Connecting to Microsoft SQL server using Python之后,我已经将pyodbc添加到我的requirements.txt,部署到Azure失败,抱怨它没有正确版本的C ++可再发行组件(9.0)可用。是否有任何可以做到这一点,或者是否需要不同的架构(如果是,那么?)?

python azure azure-sql-database pyodbc
1个回答
2
投票

我试图在我的烧瓶网络应用程序中访问Azure SQL数据库。你可以参考我的工作代码。

view.朋友

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)

enter image description here

第2步:发布您的flask项目并添加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并通过编辑按钮将网址https://bootstrap.pypa.io/get-pip.py的内容复制到get-pip.py,然后运行python get-pip.py以安装pip工具。

enter image description here

第4步:通过python -m pip install pyodbc安装pyodbc软件包或任何你需要的软件包

enter image description here

更多部署详情,请参阅此tutorial

获取查询结果

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

enter image description here

希望它可以帮助你。任何关注,请让我知道。

© www.soinside.com 2019 - 2024. All rights reserved.