简体   繁体   English

如果用户登录失败,则 Python SQL Server 登录重试逻辑

[英]Python SQL Server login retry logic if login failed for user

Python SQL Server login retry logic if login failed for user.如果用户登录失败,则 Python SQL Server 登录重试逻辑。 I am using this logic in a script, but sometimes it's failing to login to the database.我在脚本中使用此逻辑,但有时无法登录到数据库。

I want to retry to connect to the database if the connection fails.如果连接失败,我想重试连接到数据库。

try:
    cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
    cursor = cnxn.cursor()

with this the connection will be retried until success.这样,连接将重试直到成功。 do you think there should be limited number of tries?你认为应该限制尝试次数吗?

cursor = None
while cursor is None:
    try:
        
        cnxn = pyodbc.connect('DRIVER={ODBC Driver 17 for SQL Server};SERVER='+server+';DATABASE='+database+';UID='+username+';PWD='+ password)
        cursor = cnxn.cursor()
    except:
         pass

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM