-1

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() 
1
  • Where are fn_ent_ent and sn_ent_ent defined? Commented Nov 27, 2018 at 1:10

2 Answers 2

2

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()
Sign up to request clarification or add additional context in comments.

Comments

-1
import tkinter as tk
from tkinter import messagebox, ttk
import sqlite3
from datetime import datetime

# Database setup
conn = sqlite3.connect('fuel_stock.db')
c = conn.cursor()
c.execute('''CREATE TABLE IF NOT EXISTS fuel (
    id INTEGER PRIMARY KEY,
    name TEXT UNIQUE,
    stock REAL
)''')
c.execute('''CREATE TABLE IF NOT EXISTS transactions (
    id INTEGER PRIMARY KEY,
    fuel_name TEXT,
    type TEXT,
    amount REAL,
    timestamp TEXT
)''')
conn.commit()

# Functions
def add_fuel():
    name = fuel_name_entry.get()
    if not name:
        messagebox.showerror("Error", "Enter fuel name")
        return
    try:
        c.execute("INSERT INTO fuel (name, stock) VALUES (?, ?)", (name, 0))
        conn.commit()
        update_fuel_list()
        messagebox.showinfo("Success", f"Added {name}")
    except sqlite3.IntegrityError:
        messagebox.showerror("Error", "Fuel already exists")

def update_fuel_list():
    fuel_combo['values'] = [row[0] for row in c.execute("SELECT name FROM fuel").fetchall()]
    refresh_stock()

def record_transaction(transaction_type):
    fuel = fuel_combo.get()
    try:
        amount = float(amount_entry.get())
    except ValueError:
        messagebox.showerror("Error", "Enter a valid amount")
        return
    if not fuel:
        messagebox.showerror("Error", "Select fuel type")
        return

    current_stock = c.execute("SELECT stock FROM fuel WHERE name = ?", (fuel,)).fetchone()[0]
    if transaction_type == 'OUT' and current_stock < amount:
        messagebox.showerror("Error", "Insufficient stock")
        return

    new_stock = current_stock + amount if transaction_type == 'IN' else current_stock -

1 Comment

While this code may answer the question, providing additional context regarding why and/or how this code answers the question improves its long-term value.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.