__init \ \()缺少1个必需的位置参数:“receiver”?

2024-09-30 10:27:31 发布

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

我试图用sqlite3和python通过数据库获取单词的数据,但是当我试图调用read \u from \u db函数时,出现了这个错误\u init \uuu()缺少1个必需的位置参数:“receiver”。我似乎找不到发生了什么
这是密码

conn = sqlite3.connect('yeet1.db')
cursor = conn.cursor()


class Ui_WordWindow(object):


    def __init__(self, receiver):  #Inherit user-input word from another window

        self.receiver = receiver        
        print(self.receiver) #Checking if it worked correctly

    def read_From_db(self): #Read and print out data of user-input word

         cursor.execute(('SELECT * FROM mytable WHERE Meaning = ?', self.receiver))
         data = cursor.fetchall()
         print(data)




window2 = Ui_WordWindow()
window2.read_From_db()
cursor.close()
conn.close

Tags: fromselfuireaddbdatainitdef
2条回答

声明类__init__Ui_WordWindow方法,如下所示:

def __init__(self, receiver):  #Inherit user-input word from another window

它有一个参数接收器。得到的错误表明,在构造Ui_WordWindow时,应该只提供一个参数,并且该参数应该是receiver的值。你知道吗

即这条线:

window2 = Ui_WordWindow()

事实上应该是:

window2 = Ui_WordWindow(receiver)

其中receiver是receiver的有效值。你知道吗

接收方对象=“某个值”

conn = sqlite3.connect('yeet1.db')
cursor = conn.cursor()


class Ui_WordWindow(object):


    def __init__(self, receiver):  #Inherit user-input word from another window

        self.receiver = receiver        
        print(self.receiver) #Checking if it worked correctly
        #when you print self.receiver it means the value which you are passing say for example "test"  

    def read_From_db(self): #Read and print out data of user-input word

         cursor.execute(('SELECT * FROM mytable WHERE Meaning = ?', "test"))

         # now the query becomes select * from mytable where meaning = 'test'
         # the value 'test' is being passed by the receiver object and you need to provide that value

         data = cursor.fetchall()
         print(data)




window2 = Ui_WordWindow(receiver) # which has some value ex 'test'
window2.read_From_db()
cursor.close()
conn.close

你需要温习一下面向对象的方法。 尝试从以下内容阅读:python object oriented

相关问题 更多 >

    热门问题