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

时间:2019-12-11 08:54:43

标签: python sqlite

我正在尝试使用sqlite3和python通过数据库获取单词的数据,但是当我尝试调用read_from_db函数时,我遇到此错误_init __()缺少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

2 个答案:

答案 0 :(得分:1)

您可以像这样声明类__init__的{​​{1}}方法:

Ui_WordWindow

有一个参数接收器。您得到的错误表明,在构造def __init__(self, receiver): #Inherit user-input word from another window 时,您应该恰好提供一个参数,该参数应该是接收器的值。

即这行:

Ui_WordWindow

实际上应该是:

window2 = Ui_WordWindow()

其中接收者是接收者的有效值。

答案 1 :(得分:0)

接收者对象=“某些值”

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