Mark Newbie
Mark Newbie

Reputation: 27

Modify a single integer value in sqlite using Python Treeview

I am currently working on a python program(a warehouse program) with sqlite, my problem is I want to add quantity to a single item but I've searched google and found nothing. The only answer I found is to replace the whole data to a new data. I need help.

My code

import tkinter as tk
from tkinter import ttk
import sqlite3  

root = tk.Tk()
global conn, cursor
conn = sqlite3.connect('db.db')
cur = conn.cursor()

addbtn = ttk.Button(root, text = 'ADD')
addbtn.pack(side ='left')
decreasebtn = ttk.Button(root, text = 'DECREASE')
decreasebtn.pack(side ='left')
tv = ttk.Treeview(root, columns=['description', 'quantity', 'location'], show = "headings",selectmode = 'browse')
tv.heading('#0', )
tv.heading('description', text = 'Description')
tv.heading('quantity', text = 'Quantity', anchor = 'center')
tv.heading('location', text = 'Location')
tv.pack(expand = True, fill = 'both')
tv.column('description', width = 200)


cur.execute("SELECT description, quantity, location FROM 'warehouse'")
fetch = cur.fetchall()
for data in fetch:

    tv.insert('', 'end', values = data)
    cur.close()
    conn.commit()

root.mainloop()

Enter image description here

enter image description here

Upvotes: 0

Views: 213

Answers (1)

Gro
Gro

Reputation: 1683

Here is a solution, I have hard coded new values that get added at the click of the button but you can change that as I have indicated in the code comment. For delete you will have to select the node and then click on Decrease button.

from tkinter import ttk
import sqlite3  

root = tk.Tk()
global conn, cursor
conn = sqlite3.connect('db.db')
cur = conn.cursor()


tv = ttk.Treeview(root, columns=['description', 'quantity', 'location'], 
show = "headings",selectmode = 'browse')
tv.heading('#0', )
tv.heading('description', text = 'Description')
tv.heading('quantity', text = 'Quantity', anchor = 'center')
tv.heading('location', text = 'Location')
tv.pack(expand = True, fill = 'both')
tv.column('description', width = 200)


def add():
    #Hardcoded, but you can take input from user store in db and then show in here
    tv.insert('', 'end', text="id_1", values=("Shampoo", 25, "London"))


def delete():
    selected_item = tv.selection()[0] ## get selected item
    tv.delete(selected_item)
    # Delete from database if you wish  

addbtn = ttk.Button(root, text = 'ADD', command=add)
addbtn.pack(side ='left')
decreasebtn = ttk.Button(root, text = 'DECREASE', command=delete)
decreasebtn.pack(side ='left')  

cur.execute("SELECT id, description, quantity, location FROM 'warehouse'")
fetch = cur.fetchall()
for data in fetch:
    print(data)
    #'' -> should be replaced by unique ids
    tv.insert('', 'end', text=str(data[0]), values = (data[1], data[2], data[3]))
    cur.close()
    conn.commit()

root.mainloop()

For testing use the following db script

create table warehouse(
    id integer primary key autoincrement,
    description varcahr(100), 
    quantity integer, 
    location varchar(100)
);

insert into warehouse(description, quantity, location) values('Biscuits', 20, 'London');
insert into warehouse(description, quantity, location) values('Tea', 50, 'London');
insert into warehouse(description, quantity, location) values('Soap', 10, 'London');

Upvotes: 1

Related Questions