Insert Automatically generated values into Sqlite Database?

Viewed 58

I have an issue where I have a tkinter window to insert values into a SQL database.

The following code:

from tkinter import *
from tkinter import ttk
import sqlite3
from tkinter.ttk import *

root = Tk()
root.geometry('350x470')
root.title("New Order Boys Synthetic")
root.config(bg="lightskyblue3")
style = ttk.Style()

OrderNo = StringVar()
Size2 = IntVar()
Quantity = IntVar()
Delivery = StringVar()

def order_b():
    code = OrderNo.get()
    size2 = Size2.get()
    delivery = Delivery.get()
    qty = Quantity.get()

    with sqlite3.connect('Reflex Footwear GUI.sql3') as conn:
        cursor = conn.cursor()
        # Insert into Orders
        cursor.execute(r'INSERT INTO MyShoe (Factory,Planned,OrderNo,Style,DeliveryDate,Quantity,Balances) VALUES(?,?,?,?,?,?,?)', ["Reflex", timestampStr, code, "BOYS SYNTHETIC", delivery, qty, qty])
        
        updated = cursor.rowcount
        conn.commit()
        cursor.close()
        log_sheet()
        root.destroy()
        sys.exit(updated)  

label_0 = Label(root, text="Boys Synthetic", width=13,
                background="lightskyblue3", font=("bold", 20)).place(x=85, y=23)

label_on = Label(root, text="Order No:", width=20,
                 background="lightskyblue3", font=("bold", 11)).place(x=40, y=90)
entry_on = Entry(root, textvar=OrderNo, background="yellow",
                 font=("bold", 10)).place(x=160, y=90)

label_2 = Label(root, text="Delivery Date:", width=20,
                background="lightskyblue3", font=("bold", 11)).place(x=40, y=120)
entry_2 = Entry(root, textvar=Delivery, background="yellow",
                font=("bold", 10)).place(x=160, y=120)

label_b2 = Label(root, text="Size 2:", width=20,
                 background="lightskyblue3", font=("bold", 11)).place(x=40, y=180)
entry_b2 = Entry(root, textvar=Size2, background="yellow",
                 font=("bold", 10)).place(x=160, y=180)

label_b6 = Label(root, text="Total Quantity:", width=20,
                 background="lightskyblue3", font=("bold", 11)).place(x=40, y=330)
entry_b6 = Entry(root, textvar=Quantity, background="yellow",
                 font=("bold", 10)).place(x=160, y=330)

# Style of buttons
style.configure('C.TButton', font=('Arial', 12, 'bold'),
                relief="raised", foreground='red')
style.configure('S.TButton', font=('Arial', 12, 'bold'),
                relief="raised", foreground='blue')

# Inserting buttons
submit1 = Button(root, text='Submit', style='S.TButton', width=11, command=order_b).place(x=20, y=420)
exit1 = Button(root, text='Close', style='C.TButton', width=11, command=root.destroy).place(x=220, y=420)

root.mainloop()

As the issue is clearly not to insert data. My question is whether I can use the same variable inputs to create a new table that automatically inserts values based on a looped reduction.

To use an example, if I have a quantity of 2000. It would be ideal if the variable could be set to 158. This will be reduced and inserted into each row until a remainder exists in the last row.

I have tested the following:

i = 158
   reduce = qty - i
   cursor.execute(r'INSERT INTO MyShoe (Factory,Planned,OrderNo,Style,DeliveryDate,Quantity,Balances) VALUES(?,?,?,?,?,?,?)', ["Reflex", timestampStr, code, "BOYS SYNTHETIC", delivery,(qty+str(reduce), qty])

Any help and/or links would be appreciated. Thank you in advance.

0 Answers
Related