how to use tkinter with sqlite in python how to use tkinter with sqlite in python tkinter tkinter

how to use tkinter with sqlite in python


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 randomimport tkinter as tkfrom tkinter import *from tkinter import messageboximport sqlite3def 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()