如何使用Python中的按钮查看或更新数据库列表中的单个记录?

2024-04-25 22:37:17 发布

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

首先,我从一个表中选择所有记录并将它们列在我的GUI中。 在下一个记录的每一个屏幕上都有两个更新记录的按钮。我尝试了几种不同的方法,但似乎无法从要查看/更新的特定记录中获取信息。我试过在按钮上添加文本变量等

我的问题是,如何查看和/或更新我表中所有行的列表中的特定行?在

def Show_Keylog_Records(self):
            data = self.Select_Keylog_Records()
            for index, dat in enumerate(data):
                Row_ID = index
                Key_Num = dat[0]
                self.Row_ID = Frame(self.Result_Frame, bg='#333')
                Label(self.Row_ID, text=dat[0]).pack(side=LEFT, expand=YES, fill=X)
                Label(self.Row_ID, text=dat[1]).pack(side=LEFT, expand=YES, fill=X)
                Label(self.Row_ID, text=dat[2]).pack(side=LEFT, expand=YES, fill=X)
                Label(self.Row_ID, text=dat[3]).pack(side=LEFT, expand=YES, fill=X)
                Button(self.Row_ID, text='view', textvariable=Row_ID, command=self.View_Keylog_Record).pack(side=RIGHT)
                Button(self.Row_ID, text='Delete', command=self.Delete_Keylog_Record).pack(side=RIGHT, padx=5)
                self.Row_ID.pack(side=TOP, fill=X, pady=3)


def Select_Keylog_Records(self):
        c.execute("SELECT * FROM KeyLog")
        return c.fetchall() 

def View_Keylog_Record(self):
        Record = (Row_ID.get(),)
        c.execute('SELECT * FROM KeyLog WHERE index=?', Record)
        print c.fetchone()

Tags: textselfid记录recordleftfillside
1条回答
网友
1楼 · 发布于 2024-04-25 22:37:17

我认为您需要的是将index传递给函数。如果是这样,请按如下方式更改按钮实例化:

Button(self.Row_ID, text='view', command=lambda index=index: self.View_Keylog_Record(index)).pack(side=RIGHT)

^{pr2}$

注意您必须import functools才能使用第二个选项。在

对“删除”按钮进行类似更改,然后按如下方式修改函数:

def View_Keylog_Record(self, index):
    c.execute('SELECT * FROM KeyLog WHERE index=?', (index,))
    print c.fetchone()

进行相应的更改以删除\u Keylog_记录,并查看这是否适合您。在

相关问题 更多 >