0

样本1.py

class1:
    
    def function(self): 
    
        dbcursor.execute('UPDATE Table')

主文件

from sample1 import class1

inventoryDb = inventory.connect('sample.db')

dbcursor = inventoryDb.cursor()

class = class1()

class.function()

NameError:未定义全局名称“dbcursor”

是什么导致了这里的错误?还有,怎么修?谢谢!

4

1 回答 1

1

dbcursor仅在 main.py 中定义。为什么不将它作为参数传递给 sample1.py 中的函数?就像是

def function(self, dbcursor):
    dbcursor.execute('UPDATE Table')

class_ = class1()
inventoryDb = inventory.connect('sample.db')
class_.function(inventoryDb.cursor())
于 2012-04-13T04:02:13.457 回答