我需要从输入字段和组合框添加数据,但是当通过get ()
传递其值时,查询本身也被添加了(self.entry_description.get(),self.combobox.get()) `。如何解决?
self.entry_description = ttk.Entry(self)
self.entry_description.place(x=200,y=50)
self.entry_money = ttk.Entry(self)
self.entry_money.place(x=200,y=110)
self.combobox = ttk.Combobox(self, values=(u'Доходы',u'Расходы'),state='readonly')
self.combobox.current(0)
self.combobox.place(x=200,y=80)
btn_add = tk.Button(self, text='Добавить', command = BD().add_item())
btn_add.place(x=220, y=170)
btn_add.bind('<Button-1>')
btn_cancel = tk.Button(self, text='Отменить', command=lambda: self.destroy())
btn_cancel.place(x=300, y=170)
btn_cancel.bind('<Button-1>')
self.grab_set()
self.focus_set()
class BD:
def create(self):
conn = sqlite3.connect('finance.db')
cursor = conn.cursor()
cursor.execute("""CREATE TABLE IF NOT EXISTS albums (ID integer primary key,
description text,
costs text,
total real)""")
conn.commit()
def add_item(self):
cursor = sqlite3.connect('finance.db').cursor()
cursor.execute("""INSERT INTO albums(description, costs, total) VALUES (?, ?, ?)""",
(self.entry_description.get(), self.combobox.get(), self.entry_money.get()))
答案 0 :(得分:0)
当前正在发生的事情是,您正在将字符串文字传递给插入内容,而这恰好是变量名。您应该使用准备好的语句,然后绑定Python变量:
cursor = sqlite3.connect('finance.db').cursor()
cursor.execute("""INSERT INTO albums(description, costs, total) VALUES (?, ?, ?)""",
(self.entry_description.get(), self.combobox.get(), self.entry_money.get()))