无法从finally exception modu调用函数

2024-09-29 17:25:10 发布

您现在位置:Python中文网/ 问答频道 /正文

我想从finally错误检测模块调用一个函数,并传递一个变量。但它显示错误为:

TypeError: 'NoneType' object has no attribute '__getitem__'

我的代码如下:

from mysql.connector import MySQLConnection, Error
import MySQLdb
import sys
import time
import signal
#from python_mysql_dbconfig import read_db_config
#from MySQL import row

sys.setrecursionlimit(1500)

def query_with_fetchone():
    try:
        db = MySQLdb.connect(host="localhost",  # your host, usually localhost
                             user="root",  # your username
                             passwd="faheemmcfc",  # your password
                             db="python")  # name of the data base
        cur=db.cursor()
        cursor = db.cursor()
        cursor.execute("SELECT * FROM down")
        queue=0
        data= cursor.fetchone()
        lastid=data[0]
        print(data[0])
        def check(lastid):
            print lastid
            global last
            last=lastid
            while True:
                cursor.execute("Select * from down where id>%s"%(last))
                data = cursor.fetchone()
                last=data[0]
                print(data[0])
                print data[1]
                #signal.pause()
        check(lastid)
    except Error as e:
        print(e)
    finally:
        print'last',last
        time.sleep(30)
        check(last)
if __name__ == '__main__':
    query_with_fetchone()

在这里,我需要无限地运行while循环,以便在数据库中创建新条目时可以检索它。 当我从finally调用check时,它显示错误为:

/usr/bin/python2.7 /home/faheem/PycharmProjects/untitled1/test2.py 1 1
2 CCLEANER 3 FANCONTROL 4 CCLEANER 5 FANCONTROL 6 CCLEANER last 6 6
Traceback (most recent call last):   File
"/home/faheem/PycharmProjects/untitled1/test2.py", line 55, in
<module>
    query_with_fetchone()   File "/home/faheem/PycharmProjects/untitled1/test2.py", line 45, in
query_with_fetchone
    check(last)   File "/home/faheem/PycharmProjects/untitled1/test2.py", line 34, in check
    last=data[0] TypeError: 'NoneType' object has no attribute '__getitem__'

Process finished with exit code 1

请告诉我如何更正它或提供另一种方法来无限地运行while循环,而不允许它转到finally部分。 请随意更正这个问题,因为我对stackoverflow不太熟悉。你知道吗


Tags: fromimporthomedbdatacheckwithquery

热门问题