Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/351.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python For循环过早退出_Python_Sql_Python 3.x_Sqlite - Fatal编程技术网

Python For循环过早退出

Python For循环过早退出,python,sql,python-3.x,sqlite,Python,Sql,Python 3.x,Sqlite,我遇到了一个问题,pythonfor循环过早退出。循环按位置迭代SQL数据库条目 如果找到位置,它将获取这些条目的IP地址并发送配置。如果作业失败,或者在我的情况下引发异常,则在异常中,它将转到一个函数以更新状态、日期/时间。当前的预期结果是一个异常 当它返回到原始函数时,它退出循环,不转到下一个DB条目(我知道它在那里)。如果我从异常中删除该函数,则一切正常 目前的结果。它显示了从一个函数到另一个函数的循环。我应该看到两行或六行: 返回功能, 回到功能上来, 完成了, 恢复功能, 回到功能上来

我遇到了一个问题,python
for
循环过早退出。循环按位置迭代SQL数据库条目

如果找到位置,它将获取这些条目的IP地址并发送配置。如果作业失败,或者在我的情况下引发异常,则在异常中,它将转到一个函数以更新状态、日期/时间。当前的预期结果是一个异常

当它返回到原始函数时,它退出循环,不转到下一个DB条目(我知道它在那里)。如果我从异常中删除该函数,则一切正常

目前的结果。它显示了从一个函数到另一个函数的循环。我应该看到两行或六行:

返回功能,
回到功能上来,
完成了,
恢复功能,
回到功能上来,
完成了,

这是我得到的

1。知识产权
2.地位
3.位置
4.主要
选择:3
地点:测试
恢复功能,
回到功能上来,
完成了,
代码如下所示:

def db_条目(设备、状态):
int=0
当int<1时:
c、 执行('从自动化中选择*,其中设备=?',(设备,))
如果bool(c.fetchone())==True:
c、 执行(“更新自动化设置时间=?,日期时间=?,状态=?其中设备=?”(Time.strftime(“%H:%M:%S+0000”),date.today(),状态,设备))
mydb.commit()
int=int+1
其他:
c、 执行(“插入到自动化值('%s','NETCONF','POC','%s','%s','%s')”)”%(设备,状态,日期.今天(),时间.strftime(“%H:%M:%s+0000”))
mydb.commit()
int=int+1
其他:
打印(“返回功能”)
def发送单_配置(文件):
status_1=“成功”
状态_2=“失败”
打印(“\n”)
查看_数据库()
打印(“\n”)
重试次数=0
打印(“1.IP”)
打印(“2.状态”)
打印(“3.位置”)
打印(“4.Main”)
打印(“\n”)
输入\选择=输入(“选择:”)
打印(“\n”)
如果输入_选择==“3”:
位置=输入(“位置:”)
对于c.execute中的行(“从自动化中选择*,其中位置=?”,(位置,):
尝试:
m=manager.connect(第[0]行,830,“cisco”,“cisco”,{'name':'csr'})
config_file=open(file=file).read()
m、 编辑配置(配置文件,target=“running”)
db_条目(第[0]行,状态_1)
打印(“\n”)
打印(“配置完成!”)
打印(“\n”)
除属性错误外:
db_条目(第[0]行,状态_2)
打印(“返回功能”)
其他:
打印(“完成”)

这是sqlite3模块。我在这里找到了一个8年前的老答案

sqllitle3模块不允许多线程

我创建了另一个连接对象,并用它来更新DB条目

    c = mydb.cursor() # Find the location
    d = mydb.cursor() # Update the entry

            for row in c.execute('SELECT * FROM Automation WHERE Location=?', (location, )):
        try:

            device_connect(row[0])
            for capability in m.server_capabilities:
                continue

            d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_1, row[0],))
            mydb.commit()

        except AttributeError:
            d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_2, row[0],))

发生的确切属性错误是什么?
status
似乎未定义,这可能会导致try块失败:
db\u条目(第[0]行,status)
。很抱歉,状态应为status\u 1。我在代码中更改了这一点,但它仍然不起作用AttributeError:“NoneType”对象没有属性“edit_config”-发生这种情况是因为m失败,或者与设备的连接失败。目前,这是一个旁注,try块中的代码应该是最小的。目前,有很多行可能会引起AttributeError。
    c.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE 
    Device=?",(time.strftime("%H:%M:%S +0000"), date.today(), status, 
    Device))
    c = mydb.cursor() # Find the location
    d = mydb.cursor() # Update the entry

            for row in c.execute('SELECT * FROM Automation WHERE Location=?', (location, )):
        try:

            device_connect(row[0])
            for capability in m.server_capabilities:
                continue

            d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_1, row[0],))
            mydb.commit()

        except AttributeError:
            d.execute("UPDATE Automation SET Time=?, DateTime=?, Status=? WHERE Device=?",(time.strftime("%H:%M:%S +00"), date.today(), status_2, row[0],))