使用选定的树视图行更新数据库
我正在尝试通过在 Treeview 中选择特定行并编辑它来更新我的 sqlite3 数据库,这样我就可以使用主键自动增量 id 在数据库中更新它 问题:更新未应用于 sqlite3 数据库,它仅应用于 Treeview。 我尝试过以下操作:
def buy_checks_only_ payment(self): #不重要的树视图信息 self.buying_checks paymenttree['columns'] = ("?日期", "?值", "checknum"?checkid")
将值添加到树视图
def buy_addcheck(self): self.checksrecords=[] self.checkid += 1
self.buying_checkspaymenttree.insert("", 'end', values=(
self.buying_check_date_var.get(), self.buying_check_value_var.get(),
self.buying_check_num_var.get(), self.checkid,self.makevar.get(),self.sellernamevar.get(),self.Buyingdate_var.get(),self.cashepayments.get(),self.buying_nocheckpic))
对于 self.buying_checks paymenttree.get_children() 中的子项: self.checksrecords.append(self.buying_checks paymenttree.item(child)["values"])
#将树视图值添加到数据库中尝试使用主键作为 self.rowid
def buying_checksdb(self): self.conn = sqlite3.connect('汽车经销商.db') self.cursorObj = self.conn.cursor()
for self.checksrecords in self.checksrecords:
self.cursorObj.execute(
"INSERT INTO cars_buying_checksonly (checkdate, checkvalue,
checknum, carmake, Sellername, buyingdate, entirepaymentmethod,
checkpic)values(?,?,?,?,?,?,?,?)",
(self.checksrecords[0], self.checksrecords[1],
self.checksrecords[2], self.checksrecords[3],
self.checksrecords[4], self.checksrecords[5],
self.checksrecords[6], self.checksrecords[7]))
self.conn.commit()
enter code here
self.rowid=self.cursorObj.lastrowid
print(self.rowid)
#尝试选择该行并在树视图中编辑它以在数据库中更新它 def buy_editcheck(self): self.conn = sqlite3.connect('汽车经销商.db') self.cursorObj = self.conn.cursor()
self.buying_selected_check_edit =
self.buying_checkspaymenttree.selection()[0]
print(self.buying_checkspaymenttree.item(self.buying_selected_check_edit)['values'])
uid =
self.buying_checkspaymenttree.item(self.buying_selected_check_edit)
['values'][0]
self.buying_checkspaymenttree.item(self.buying_selected_check_edit,
values=(
=self.buying_check_date_var.get(),self.buying_check_value_var.get(),
self.buying_check_num_var.get(),self.checkid))
#使用 self.rowid 而不是 self.checkid 有帮助吗?
self.cursorObj.execute("UPDATE cars_buying_checksonly SET
checkdate=?,checkvalue=?, checknum=? WHERE uid=?",
(self.buying_check_date_var.get(),self.buying_check_value_var.get(),
self.buying_check_num_var.get(),uid,))
self.conn.commit()
重要提示:如果您可以帮助发布您的答案,发布您的评论,如果不能,请不要打扰我如何发布我的代码,在这里发布我的问题是最后的选择,我已经被困在这个有一段时间了并使用我用尽所有精力来解决这个问题,当我失去希望时,我将其发布在这里,所以如果您不想帮助我,请不要向我发送任何可能雪上加霜的内容
I am trying to update my sqlite3 database by selecting a specific row in the Treeview and editing it, so I can update it in the database using primary key autoincrement id
the problem: The update is not applied to the sqlite3 database it only applied to Treeview.
I have tried the following:
def buying_checks_only_payment(self):
#unimportant treeview info's
self.buying_checkspaymenttree['columns'] = ("اdate", "اvalue",
"checknum"اcheckid")
add values into treeview
def buying_addcheck(self):
self.checksrecords=[]
self.checkid += 1
self.buying_checkspaymenttree.insert("", 'end', values=(
self.buying_check_date_var.get(), self.buying_check_value_var.get(),
self.buying_check_num_var.get(), self.checkid,self.makevar.get(),self.sellernamevar.get(),self.Buyingdate_var.get(),self.cashepayments.get(),self.buying_nocheckpic))
for child in self.buying_checkspaymenttree.get_children():
self.checksrecords.append(self.buying_checkspaymenttree.item(child)["values"])
#add treeview values into database trying to use the primry key as self.rowid
def buying_checksdb(self):
self.conn = sqlite3.connect('car dealership.db')
self.cursorObj = self.conn.cursor()
for self.checksrecords in self.checksrecords:
self.cursorObj.execute(
"INSERT INTO cars_buying_checksonly (checkdate, checkvalue,
checknum, carmake, Sellername, buyingdate, entirepaymentmethod,
checkpic)values(?,?,?,?,?,?,?,?)",
(self.checksrecords[0], self.checksrecords[1],
self.checksrecords[2], self.checksrecords[3],
self.checksrecords[4], self.checksrecords[5],
self.checksrecords[6], self.checksrecords[7]))
self.conn.commit()
enter code here
self.rowid=self.cursorObj.lastrowid
print(self.rowid)
#trying to select the row and edit it in treeview to update it in the database
def buying_editcheck(self):
self.conn = sqlite3.connect('car dealership.db')
self.cursorObj = self.conn.cursor()
self.buying_selected_check_edit =
self.buying_checkspaymenttree.selection()[0]
print(self.buying_checkspaymenttree.item(self.buying_selected_check_edit)['values'])
uid =
self.buying_checkspaymenttree.item(self.buying_selected_check_edit)
['values'][0]
self.buying_checkspaymenttree.item(self.buying_selected_check_edit,
values=(
=self.buying_check_date_var.get(),self.buying_check_value_var.get(),
self.buying_check_num_var.get(),self.checkid))
#is using self.rowid instead of self.checkid can help?
self.cursorObj.execute("UPDATE cars_buying_checksonly SET
checkdate=?,checkvalue=?, checknum=? WHERE uid=?",
(self.buying_check_date_var.get(),self.buying_check_value_var.get(),
self.buying_check_num_var.get(),uid,))
self.conn.commit()
Important: if you can help post your answer post your comment, if you can't please do not bother me with how to post my code, posting my question here was the last choice, I have been stuck in this for a while and used all my energy to solve it and when I Lost the hope, I posted it here, so please if you don't want to help me with it don't send me anything that could add insult to injury
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
解决方案是:
结论:使用自动增量id编辑您的代码或删除它并使用具有id值的列选择您的行。
Solution is:
Conclusion: Using autoincrement id to edit your code or delete it and selected your row by using the column that has the id value.