Reputation: 21
I had issues with delivering tkinter entry to sqlite My goal is build user interface to collect data and delete, show, update, I will keep learning.
I think my problem in
def savedata ():
I changed what inside brackets
I tried change this also
c.execute('INSERT INTO data (fname, sname) VALUES (?,?)',
(firstname_entry, secondnamename_entry))
conn.commit()
.
thank for helping
.
import random
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import sqlite3
def conacona():
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS stud (firstname TEXT, secondname TEXT)")
conn.commit()
conn.close()
#oooooooo
main_menu = tk.Tk()
firstname_label = Label(main_menu, text="First name")
firstname_label.pack()
secondname_label = Label(main_menu, text="Second name")
secondname_label.pack()
# First name get
firstname_entry = tk.StringVar()
firstname_entry_entry = Entry(main_menu, textvariable = fn_ent_ent)
firstname_entry_entry.pack()
# Second name get
secondname_entry = tk.StringVar()
secondname_entry_entry = Entry(main_menu, textvariable = sn_ent_ent)
secondname_entry_entry.pack()
def savedata ():
conn = sqlite3.connect('stud.db')
c = conn.cursor()
c.execute('INSERT INTO data (fname, sname) VALUES (?,?)', (firstname_entry, secondnamename_entry))
conn.commit()
print("OK")
u_ent_btn = Button(text="Enter",command=savedata())
u_ent_btn.pack()
main_menu.mainloop()
Upvotes: 1
Views: 8104
Reputation: 87054
Replace fn_ent_ent
with firstname_entry
and sn_ent_ent
with secondname_entry
to refer to the string variables that you created.
There is a typo in the execute()
statement: it should be secondname_entry
, not secondnamename_entry
. Also you need to call .get()
on the string variables to retrieve the value to be used in the query.
The SQL statement must reference the correct table and column names that were used when the table was created namely stud
instead of data
, andfirstname
and secondname
instead of fname
and sname
.
c.execute('INSERT INTO stud (firstname, secondname) VALUES (?,?)', (firstname_entry.get(), secondname_entry.get()))
Do not call savedata()
when passing it as the function for the button command
:
u_ent_btn = Button(text="Enter",command=savedata)
Finally, you need to call conacona()
to create the SQLite database before entering the mainloop()
. And you must use the same file name for the database, so make it one of stud.db
or student.db
but not both.
Putting all of that together results in this code:
import random
import tkinter as tk
from tkinter import *
from tkinter import messagebox
import sqlite3
def conacona():
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute("CREATE TABLE IF NOT EXISTS stud (firstname TEXT, secondname TEXT)")
conn.commit()
conn.close()
#oooooooo
main_menu = tk.Tk()
firstname_label = Label(main_menu, text="First name")
firstname_label.pack()
secondname_label = Label(main_menu, text="Second name")
secondname_label.pack()
# First name get
firstname_entry = tk.StringVar()
firstname_entry_entry = Entry(main_menu, textvariable=firstname_entry)
firstname_entry_entry.pack()
# Second name get
secondname_entry = tk.StringVar()
secondname_entry_entry = Entry(main_menu, textvariable=secondname_entry)
secondname_entry_entry.pack()
def savedata ():
print(dir(firstname_entry))
conn = sqlite3.connect('student.db')
c = conn.cursor()
c.execute('INSERT INTO stud (firstname, secondname) VALUES (?,?)', (firstname_entry.get(), secondname_entry.get()))
conn.commit()
print("OK")
u_ent_btn = Button(text="Enter",command=savedata)
u_ent_btn.pack()
conacona()
main_menu.mainloop()
Upvotes: 2