Python Forum
[Tkinter] data enterred through gui is not storing in sqlite3 database
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
[Tkinter] data enterred through gui is not storing in sqlite3 database
#1
Question 
Please help me with my code.
Here is my code. And below this code, the database file is attached.
import sqlite3

from tkinter import *        #importing tkinter for GUI creation
from tkinter import messagebox
con = sqlite3.connect('question_bank')

def search():                 #method for searching database bodules
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="select * from modulename where moduleno=?",moduleno.get()
        cur.execute(sql)
        result=cur.fetchone()
        name.set(result[1])
        """el.configure(state='disabled')"""
        con.close()
    except:
        messagebox.showinfo('No Data', 'No such data available...')
    finally:
        clear()
def clear():
    moduleno.set('')
    name.set('')
    """el.configure(state='normal')"""

def add():                       #method for adding into database bodules
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="insert into modulename values(?, ?)",(moduleno.get(), name.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record saved...')
    except:
        messagebox.showinfo('Error', 'Error in data entry...')
    finally:
        clear()

def update():                           #method for updating database bodules
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="update modulename set name=? where moduleno=?",(name.get(), moduleno.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record updated...')
    except:
        messagebox.showinfo('Error', 'Error occured...')
    finally:
        clear()

def delete():                        #method for deleting database bodules
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="delete from modulename where moduleno=?",(moduleno.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record deleted...')
    except:
        messagebox.showinfo('Error', 'Error occured...')
    finally:
        clear()






w1=Tk()
w1.title('For Administration')
w1.geometry('500x550')
ptitle=Label(w1, text= '''Add, delete and modify the records
                         from the modules table''')

ptitle.grid(row=0, column=0, columnspan=2)

moduleno=StringVar()
name=StringVar()

l1=Label(w1, text = 'ModuleNo')
e1=Entry(w1, textvariable=moduleno)

l2=Label(w1, text = 'Name')
e2=Entry(w1, textvariable=name)
                                #buttons for modules updating
b1=Button(w1, text = 'Search', command=search)
b2=Button(w1, text = 'Add', command=add)
b3=Button(w1, text = 'Update', command=update)
b4=Button(w1, text = 'Delete', command=delete)
b5=Button(w1, text = 'Clear', command=clear)

l1.grid(row=1, column=0)
e1.grid(row=1, column=1)
b1.grid(row=1, column=2)

l2.grid(row=2, column=0)
e2.grid(row=2, column=1)

b2.grid(row=3, column=0)
b3.grid(row=3, column=1)
b4.grid(row=4, column=0)
b5.grid(row=4, column=1)


#Accessing and editing questions table in our question_bank database

def search():                    #method for searching database questions
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="select * from questions where questionno=?",questionno.get()
        cur.execute(sql)
        result=cur.fetchone()
        text.set(result[1])
        """el.configure(state='disabled')"""
        con.close()
    except:
        messagebox.showinfo('No Data', 'No such data available...')
        clear()
def clear():
    questionno.set('')
    text.set('')
    """el.configure(state='normal')"""

def add():                          #method for adding into database questions
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="insert into questions values(?, ?)",(questionno.get(), text.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record saved...')
    except:
        messagebox.showinfo('Error', 'Error in data entry...')
    finally:
        clear()

def update():                   #method for updating database questions
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="update questions set text=? where questionno=?",(text.get(), questionno.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record updated...')
    except:
        messagebox.showinfo('Error', 'Error occured...')
    finally:
        clear()

def delete():                       #method for deleting database questions
    try:
        con = sqlite3.connect('question_bank')
        cur=con.cursor()
        sql="delete from questions where questionno=?",(questionno.get())
        cur.execute(sql)
        con.commit()
        con.close()
        messagebox.showinfo('Success', 'Record deleted...')
    except:
        messagebox.showinfo('Error', 'Error occured...')
    finally:
        clear()









ptitle=Label(w1, text= '''Add, delete and modify the questions
                         from the modules''')

ptitle.grid(row=5, column=0, columnspan=2)

questionno=StringVar()
text=StringVar()

l1=Label(w1, text = 'QuestionNo')
e1=Entry(w1, textvariable=questionno)

l2=Label(w1, text = 'Text')
e2=Entry(w1, textvariable=text)
                        #buttons for questions database editing
b6=Button(w1, text = 'Search', command=search)
b7=Button(w1, text = 'Add', command=add)
b8=Button(w1, text = 'Update', command=update)
b9=Button(w1, text = 'Delete', command=delete)
b10=Button(w1, text = 'Clear', command=clear)

l1.grid(row=6, column=0)
e1.grid(row=6, column=1)
b6.grid(row=6, column=2)

l2.grid(row=7, column=0)
e2.grid(row=7, column=1)

b7.grid(row=8, column=0)
b8.grid(row=8, column=1)
b9.grid(row=9, column=0)
b10.grid(row=9, column=1)

w1.mainloop()
Reply
#2
Help how?
Reply
#3
The information i enter in the gui does not get stored in sqlite3 database. I previously used MySql database which was working but when I changed the database to sqlite3 it stopped working. Also the update functions at line 40 and line 143 are not working. These functions are for updating an existing data but they don't work. I have submitted the code for whom I have written it but I want to improve it and learn my mistake. Please help me with that.
(Nov-30-2021, 05:48 AM)deanhystad Wrote: Help how?
Reply
#4
Sounds like the problem is with your database calls, not the GUI. This looks suspicious to me.
        sql="insert into modulename values(?, ?)",(moduleno.get(), name.get())
        cur.execute(sql)
Does execute() take a tuple as an argument? I don't think so. If we assume moduleno.get() returns "A" and name.get() returns "B", the result of the sql assignment above is a tuple ("insert into modulename values(?, ?)", not "insert into modulename values(A, B)". The execute() command replaces question marks with argument values, not Python.
sql="insert into modulename values(?, ?)"
args = (moduleno.get(), name.get())
cur.execute(sql, args)
or more simply.
cur.execute("insert into modulename values(?, ?)", (moduleno.get(), name.get()))
Reply
#5
I put both of your formats in my code my none of them worked. Thank you for your time and efforts. waiting for another fix,
Reply
#6
There are a lot of problems here. I think it would be best if you started over and forget about doing any GUI work until you understand how sqlite3 works.

I removed all you exception handling and that let me see what errors occurred. When writing code I think I use the default error handling until I have everything working and I know what exceptions to catch. You should never use "except:"

When I tried to add something to the question_bank I got this error:
Error:
sqlite3.OperationalError: no such table: modulename
This is to be expected since I don't have an existing database and there is no code in your example that creates a "modulename" table.
Reply
#7
(Dec-01-2021, 04:14 AM)deanhystad Wrote: There are a lot of problems here. I think it would be best if you started over and forget about doing any GUI work until you understand how sqlite3 works.

I removed all you exception handling and that let me see what errors occurred. When writing code I think I use the default error handling until I have everything working and I know what exceptions to catch. You should never use "except:"

When I tried to add something to the question_bank I got this error:
Error:
sqlite3.OperationalError: no such table: modulename
This is to be expected since I don't have an existing database and there is no code in your example that creates a "modulename" table.
Yes, you are right there is no code that creates a database but I have made an sqlite database for my code using desktop apps and I am not getting any database error when I run my code in fact my compiler doesn't show me any error, my code runs fine but when I enter data in database using GUI it doesn't work and GUI shows the error message box.
Reply
#8
The error message box only pops up if your code raises an exception (the opposite of running fine). I suggest you remove all the try/excepts from your code and let Python print out the error and error trace. That will give you more information about the cause of the errors. Once you have the code working you can put the try/except's back in and display a more informative error message.

And why does your posted code have 2 of every function?

What is the name of your database file? When I run your code it creates a file 'question_bank' in the current working directory. No extension.
Reply
#9
ok I will do as you said.
My database name is question_bank, with two tables one is modulename which is used to store name of courses and one is questions which is used to store questions.
Also I want to add questions to questions table but under a specific course stored in modulename table. If you can help me add such statements to my code I will be grateful.
Reply
#10
(Dec-01-2021, 05:07 PM)deanhystad Wrote: The error message box only pops up if your code raises an exception (the opposite of running fine). I suggest you remove all the try/excepts from your code and let Python print out the error and error trace. That will give you more information about the cause of the errors. Once you have the code working you can put the try/except's back in and display a more informative error message.

And why does your posted code have 2 of every function?

What is the name of your database file? When I run your code it creates a file 'question_bank' in the current working directory. No extension.

Please help me add classes in my code. I have 2 of every function because one time for adding a course and another for adding questions but as I asked in above comment to I want to add questions to a specific course/module in separate table which I have named as questions. And how can I reuse my code to avoid duplication of functions.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [Tkinter] Error verify data in database TomasSanchexx 2 909 Aug-11-2023, 12:37 PM
Last Post: TomasSanchexx
  PyQt5 form not displaying my data from SQLite3 Database Linuxdesire 2 4,987 Jan-10-2023, 09:51 PM
Last Post: gradlon93
  [Tkinter] TKINTER quiz using sqlite3 database hezza_23 45 21,482 Nov-29-2021, 09:42 PM
Last Post: Hilal
  [Tkinter] load sqlite3 data into pdf rwahdan 5 4,376 Nov-29-2021, 07:58 PM
Last Post: Hilal
  Button to add data to database and listbox SalsaBeanDip 1 2,882 Dec-06-2020, 10:13 PM
Last Post: Larz60+
  Help with PySimpleGUI INSERT INTO sqlite3 database jrbond 5 7,180 Jul-20-2020, 01:24 PM
Last Post: jrbond
  [Tkinter] Displaying Data from a database and run a function when clicked? PythonNPC 1 2,070 Mar-11-2020, 08:16 PM
Last Post: Larz60+
  [Tkinter] I need to know how to put my SQLite3 data into a Treeview Thename921 1 7,120 Jan-12-2019, 10:26 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020