python - Azure 上的 pyodbc

标签 python azure azure-sql-database pyodbc

我在 Azure 中有一个 python web 应用程序,我想从同一资源组中的 Azure 中的 SQLServer 读取该应用程序。按照这个例子Connecting to Microsoft SQL server using Python ,我已将 pyodbc 添加到我的 requests.txt 中,部署到 Azure 失败,提示它没有可用的正确版本的 C++ 可再发行组件 (9.0)。有什么办法可以解决这个问题,或者是否需要不同的架构(如果需要,是哪个?)?

最佳答案

我尝试在我的 Flask Web 应用程序中访问 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)

enter image description here

第 2 步:发布您的 flask 项目并添加 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 进行安装点工具。

enter image description here

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

enter image description here

更多部署详情,请引用此tutorial .

获取查询结果

访问 URL http://***.azurewebsites.net/database

enter image description here

希望对您有帮助。如有任何疑问,请告诉我。

关于python - Azure 上的 pyodbc,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49087407/

相关文章:

python - 网络驱动程序异常 : Message: 'geckodriver' executable needs to be in PATH

如果元组中的第二项是顺序的,Python 从元组中提取索引

python - sklearn.model_selection.cross_val_score 与在混淆矩阵上进行的手动计算有不同的结果

excel - 是否有任何自动方法可以将 Azure SQL 数据库表数据导出到 Excel?

azure - 无法创建从 Azure Blob 存储容器到 Azure Sql 数据库超大规模的外部数据源

azure - 将新的 SQL Azure 层与旧层进行比较

python - 在 Python 中将函数返回值作为方法参数传递

javascript - 如何在没有门户的情况下更改 Azure Web 应用程序中的启动命令?

azure - 无法在 VS2015 中开始调试 Azure Web 应用程序

azure - 如何限制/限制 Azure Active Directory 中的 CallRecords.read.all API 权限