使用选定的树视图行更新数据库

发布于 2025-01-10 14:05:00 字数 2664 浏览 0 评论 0原文

我正在尝试通过在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

初懵 2025-01-17 14:05:00

解决方案是:

self.buying_checkspaymenttree.item(self.buying_selected_check_edit) 
['values'][0], # specifying column that id value inserted in, in my situation 
['values'][3], # due to the language that I am using in the GUI, 
               # I am using a language from Right to left.

结论:使用自动增量id编辑您的代码或删除它并使用具有id值的列选择您的行。

Solution is:

self.buying_checkspaymenttree.item(self.buying_selected_check_edit) 
['values'][0], # specifying column that id value inserted in, in my situation 
['values'][3], # due to the language that I am using in the GUI, 
               # I am using a language from Right to left.

Conclusion: Using autoincrement id to edit your code or delete it and selected your row by using the column that has the id value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文