I googled a lot but it didn't work. Posted a question last week but no answer as it seems that it was too lengthily...
Hopefully, the new question is much clearer.
This is just a small piece of the code and if you run it you will be able to reproduce the issue. What I need to do basically is to get user input (E1 from mainGUI class) and pass it to insert() function from Database class. The error I get when I try to add an entry is:
"self.curs.execute("INSERT INTO diary VALUES (?)", (date)) sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 1, and there are 0 supplied."
I can read from the database with no problems.
Any help would be much appreciated.
from tkinter import *
import sqlite3
class mainGUI(object):
def __init__(self,window):
self.window=window
self.E1 = Entry(window)
self.E1.grid(row=1, column=0)
self.EG1 = self.E1.get()
global E4
E4 = Listbox(window)
E4.grid(row=2)
B1 = Button(window, text="Add entry", command=lambda: database.insert(self.EG1))
B1.grid(row=1, column=4)
B2 = Button(window, text="View all", command=database.view_all)
B2.grid(row=2, column=4, sticky="WN")
window.mainloop()
class Database(mainGUI):
def __init__(self, db):
self.conn = sqlite3.connect(db)
self.curs = self.conn.cursor()
self.curs.execute("CREATE TABLE IF NOT EXISTS diary (date TEXT)")
self.conn.commit()
def insert(self,date):
database.add_entry(date)
E4.delete(0, END)
E4.insert(END,(date))
def add_entry(self,date):
self.curs.execute("INSERT INTO diary VALUES (?)", (date))
self.conn.commit()
def view_all(self):
E4.delete(0, END)
self.curs.execute("SELECT * FROM diary")
data = self.curs.fetchall()
for row in data:
E4.insert(END,row)
if __name__ == "__main__":
database = Database("dbase.db")
window=Tk()
gui = mainGUI(window)
self.E1.get()on button click, to get the current value. If you do it in the__init__method then it will just be an empty string.