Azure 上的 pyodbc [英] pyodbc on Azure

查看:21
本文介绍了Azure 上的 pyodbc的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 Azure 中有一个 python webapp,我想从同一资源组中的 Azure 中的 SQLServer 读取它.按照这个示例

第 2 步:发布您的 flask 项目并添加 web.config.

web.config:

.app"/><add key="PYTHONPATH" value="D:homesitewwwroot"/><add key="WSGI_LOG" value="D:homeLogFileswfastcgi.log"/></appSettings><system.webServer><处理程序><add name="PythonHandler" path="*" verb="*" modules="FastCgiModule" scriptProcessor="D:homePython361x64python.exe|D:homePython361x64wfastcgi.py" resourceType=未指定" requireAccess="脚本"/></处理程序></system.webServer></配置>

第三步:切换到Kudu CMD命令cd Python361x64touch get-pip.py,复制url内容https://bootstrap.pypa.io/get-pip.py通过Edit按钮进入get-pip.py,然后运行python get-pip.py进行安装pip 工具.

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

更多部署细节,请参考这个

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

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?)?

解决方案

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
    )

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>

Install pyodbc package

My web app works with python361x64 extension. Please refer to the steps I did as below:

Step 1 : Create azure web app and add Extensions(here is Python 3.6.1 x64)

Step 2 : Publish your flask project and add the 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>

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.

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

Access the url http://***.azurewebsites.net/database .

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

这篇关于Azure 上的 pyodbc的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆