我希望我没有重复发帖,我还没有找到解决我的问题的方法。我是一名初级开发人员,目前正在尝试将现有网站的新版本投入生产。我使用版本 3.9.13 的 python 工作,并尝试通过 Azure 函数访问数据库(在 Azure 上)。 当我在本地主机上工作时,一切都很好,但是当我将本地前端连接到后端的已部署开发版本时,出现以下错误:
('01000', "[01000] [unixODBC][Driver Manager]Can't open lib 'ODBC Driver 17 for SQL Server' : file not found (0) (SQLDriverConnect)")
按照程序,它尝试连接数据库 5 次,然后崩溃。 当我在连接到部署的后端开发版本的网站的已部署前端开发版本上时,会出现相同的事件。它崩溃了 Err 500。
我不明白为什么,因为我已经在我的计算机上下载了正确版本的ODCB,它可以在正在运行的主分支上运行,它位于部署期间加载的requirements.txt文档中。我还没有接触过它,但这是与数据库的连接的编码方式:
def connect_to_database(server, database, driver, DEBUG_MODE):
logging.info("ENTERING connect_to_database")
logging.info(f"Driver: {driver}")
logging.info(f"Database: {database}")
logging.info(f"Server: {server}")
conn = None
retry_flag = True
retry_count = 0
connection_string = (
f"Driver={driver};Server=tcp:{server};PORT=xxxx;Database={database};"
"Encrypt=yes;TrustServerCertificate=no;Connection Timeout=5;"
)
if DEBUG_MODE:
connection_string += "[email protected];Authentication=ActiveDirectoryInteractive"
else:
connection_string += "Authentication=ActiveDirectoryMsi"
while retry_flag and retry_count < 5:
try:
logging.info("Attempting to connect to database...")
pyodbc.pooling = True
conn = pyodbc.connect(connection_string)
retry_flag = False
logging.info("Database connection successful.")
except Exception as e:
logging.error(f"Retry {retry_count + 1} failed: {e}")
retry_count += 1
time.sleep(1)
if conn:
logging.info("DB connection OK")
else:
logging.error("Failed to connect to the database after retries")
return conn
def connect_to_MPAPPS():
logging.info("ENTERING connect_to_MPAPPS")
conn = connect_to_MPAPPS_Conn()
if conn:
logging.info(f"conn result: {conn}")
cursor = conn.cursor()
logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS")
return cursor
else:
logging.error("Failed to obtain a database connection")
return None
def connect_to_MPAPPS_Conn():
logging.info("ENTERING connect_to_MPAPPS_Conn")
server = "server address"
logging.info(f"Server: {server}")
database = "database_name"
logging.info(f"Database: {database}")
driver = "{ODBC Driver 17 for SQL Server}"
logging.info(f"Driver: {driver}")
DEBUG_MODE = 1 if os.getenv("AZURE_FUNCTIONS_ENVIRONMENT") == "Development" else 0
conn = connect_to_database(server, database, driver, DEBUG_MODE)
logging.info(f"conn: {conn}")
logging.info("SUCCESSFULLY EXITING connect_to_MPAPPS_Conn")
return conn
我尝试了一切可能的console.logging来更好地理解这个问题,我尝试从Postman调用,我到处搜索,我检查了我在Azure中可以访问的所有变量,我检查了SQL驱动程序是否相同作为主要的(它是),我可以看到它下载到我的计算机上,我还没有发现主要和开发之间有不同的变量...
如果我提供的信息不完整,请随时提出任何问题。
在部署回来时找不到 ODBC 驱动程序 17,但可以在本地主机中工作
ODBC 驱动程序 17 目前(截至目前)无法与 Portal 中的 azure 功能配合使用。
使用 ODBC 驱动程序 17 :
有一个新的更新,ODBC Deriver 18,它正在门户中使用 azure 函数:
f"Driver={{ODBC Driver 18 for SQL Server}};Server={ri_s1};Database={db1};Uid={ri_un};Pwd={r_passwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
示例代码:
import logging as rilg
import azure.functions as func
import pyodbc
def main(req: func.HttpRequest) -> func.HttpResponse:
rilg.info('Hello Rithwik Bojja, Azure Function is Started')
ri_un = 'rithwikusrename'
r_passwd = 'pssword'
ri_s1 = 'rithwik.database.windows.net'
db1 = 'dbanme'
ri_cs = f"Driver={{ODBC Driver 18 for SQL Server}};Server={ri_s1};Database={db1};Uid={ri_un};Pwd={r_passwd};Encrypt=yes;TrustServerCertificate=no;Connection Timeout=30;"
with pyodbc.connect(ri_cs) as ri_cn:
with ri_cn.cursor() as ri:
ri.execute('SELECT SYSTEM_USER;')
rith = ri.fetchone()
print("Hello Rithwik Bojja the User is : ",rith)
rilg.info('Hello Rithwik Bojja, Azure Function is Executed')
return func.HttpResponse(f"Hello Rithwik Bojja the User is: {rith}", status_code=200)
需求.txt:
azure-functions
pyodbc
输出: