【问题标题】:Importing pyodbc in app.py causes azure web app to fail在 app.py 中导入 pyodbc 导致 azure web 应用程序失败
【发布时间】:2023-02-02 18:52:32
【问题描述】:

要求.txt

click==8.1.3
Flask==2.2.2
itsdangerous==2.1.2
Jinja2==3.1.2
MarkupSafe==2.1.2
pyodbc==4.0.35
Werkzeug==2.2.2

应用程序.py

import pyodbc
from flask import Flask, render_template

#def get_db_connect():
#    conn = pyodbc.connect('Driver={ODBC Driver 18 for SQL Server};Server=tcp:servername.database.windows.net,1433;Database=Dev-testing;Uid=username;Pwd={supersecurepassword};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;')
#    return conn

app = Flask(__name__)

@app.route('/')
def index():
#    conn = get_db_connect()
#    assets = conn.execute('SELECT * FROM chosen_table').fetchall()
#    conn.close()
    return render_template('index.html')

如果我注释掉导入,它会生成基页并正常工作。但是进行该导入会导致容器崩溃。任何帮助将不胜感激。

我需要建立到 Azure SQL 实例的数据库连接。我试图按照教程进行操作,但似乎没有任何效果。

【问题讨论】:

  • 安装时如何构建 pyODBC?好像它只是没有安装。
  • 我使用 github 操作来构建和部署。我允许 Azure 创建工作流。
  • 请添加构建脚本(或标记文件)和您在上面使用的构建日志。

标签: python-3.x azure azure-web-app-service pyodbc


【解决方案1】:

首先,我在本地安装了 pyodbcpip 安装 pyodbc

我需要建立到 Azure SQL 实例的数据库连接。我试图按照教程进行操作,但似乎没有任何效果。

根据上面的评论,我在门户中创建了 Azure SQL 数据库。

然后,我创建了一个 python 应用程序,通过导入连接 Azure SQL 服务器数据库pyodbc

from  flask  import  Flask, render_template

import  pyodbc

  

app = Flask(__name__)

  

server = 'tcp:***.database.windows.net'

database = '****'

username = '****'

password = '*****'

  

cnxn = pyodbc.connect('DRIVER={ODBC Driver 18 for SQL Server};SERVER='+server+';DATABASE='+database+';ENCRYPT=yes;UID='+username+';PWD='+ password)

cursor = cnxn.cursor()

  

@app.route('/')

def  index():

cursor.execute("select current_timestamp;")

row = cursor.fetchone()

return  'Current Timestamp: ' + str(row[0])

  

if  __name__ == '__main__':

app.run()

我开始在本地运行我的应用程序,它在本地运行良好。

然后我在 Azure Web 应用程序中部署了我的应用程序。

部署到 Azure Web App 后也运行良好。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-10
    • 1970-01-01
    • 2019-04-30
    • 1970-01-01
    相关资源
    最近更新 更多