如何从 mysql table 中删除 python 树视图中的选定项目?
How to delete selected item on python treeview from the mysql table?
我试图同时从树视图和数据库 Mysql 中删除选定的记录。将记录从数据库插入到树视图并从树视图中删除选定的记录。
但我的问题是如何同时从数据库中删除选定的记录?
tv=ttk.Treeview(manage,columns=(1,2,3,4,5),show="headings",height="25")
tv.insert('','end',values=i)
建议将 iid
选项设置为 table 中的 ID
:
for i in rows:
tv.insert('', 'end', iid=i[0], values=i) # assume ID is the first column in the table
然后你可以删除table里面removeRecord()
的记录如下:
def removeRecord():
selected = tv.selection()
if selected:
# a row is selected
x = selected[0]
tv.delete(x)
# delete record from table
sql = 'DELETE FROM employeeInfo WHERE ID = %s'
mycursor.execute(sql, (x,))
# assume mydb is the connection object of MySQL database
mydb.commit() # make sure to commit the change
我试图同时从树视图和数据库 Mysql 中删除选定的记录。将记录从数据库插入到树视图并从树视图中删除选定的记录。
但我的问题是如何同时从数据库中删除选定的记录?
tv=ttk.Treeview(manage,columns=(1,2,3,4,5),show="headings",height="25")
tv.insert('','end',values=i)
建议将 iid
选项设置为 table 中的 ID
:
for i in rows:
tv.insert('', 'end', iid=i[0], values=i) # assume ID is the first column in the table
然后你可以删除table里面removeRecord()
的记录如下:
def removeRecord():
selected = tv.selection()
if selected:
# a row is selected
x = selected[0]
tv.delete(x)
# delete record from table
sql = 'DELETE FROM employeeInfo WHERE ID = %s'
mycursor.execute(sql, (x,))
# assume mydb is the connection object of MySQL database
mydb.commit() # make sure to commit the change