how to .get() all entry box insert into sqlite 3 in python tkinter
I have Create loop entry boxes
I have try all entry boxes .get() and insert into sqlite 3
but insert only last three entry boxes only
I have insert all boxes into sqlite 3
all entry boxes insert into sqlite 3 in single row
import sqlite3
#------------------------------------
class sample:
def __init__(self, root):
self.root = root
self.all_entries = []
showButton = Button(root, text='Show all text', command=self.showEntries)
showButton.pack()
addboxButton = Button(root, text='<Add Time Input>', fg="Red", command=self.addBox)
addboxButton.pack()
savee = Button(root, text='Save', fg="green", command=self.save)
savee.pack()
def save(self):
print(self.all_entries)
con = sqlite3.connect(database=r'data.db')
cur = con.cursor()
cur.execute("Insert into e_transations (sl,id,datee) values(?,?,?)", (
self.ent1.get(), self.ent2.get(), self.ent3.get()
))
con.commit()
def addBox(self):
frame = Frame(root)
frame.pack()
Label(frame, text='From').grid(row=0, column=0)
self.ent1 = Entry(frame)
self.ent1.grid(row=1, column=0)
Label(frame, text='To').grid(row=0, column=1)
self.ent2 = Entry(frame)
self.ent2.grid(row=1, column=1)
self.ent3 = Entry(frame)
self.ent3.grid(row=1, column=2)
self.all_entries.append((self.ent1, self.ent2,self.ent3))
# ------------------------------------
def showEntries(self):
for number, (self.ent1,self.ent2,self.ent3) in enumerate(self.all_entries):
print(number, self.ent1.get(), self.ent2.get(), self.ent3.get())
# ------------------------------------
root = Tk()
obj = sample(root)
root.mainloop()
CodePudding user response:
save()
should save the values of all the entries in self.all_entries
, not just the last self.ent1
, self.ent2
, and self.ent3
.
def save(self):
print(self.all_entries)
con = sqlite3.connect(database=r'data.db')
cur = con.cursor()
values = [[entry.get for entry in entry_set] for entry_set in self.all_entries]
cur.executemany("Insert into e_transations (sl,id,datee) values(?,?,?)", values)
con.commit()
self.all_entries
should contain the values from .get()
of the entry widgets, not