Hướng dẫn python update query not working - truy vấn cập nhật python không hoạt động

Vì vậy, tôi đang tạo một hệ thống quản lý cửa hàng sử dụng cơ sở dữ liệu SQLite. Khi tôi thực hiện truy vấn trực tiếp trong trình duyệt SQLite, nó hoạt động, nhưng khi tôi sử dụng Python, không có lỗi, nhưng bản cập nhật không xảy ra. Bạn có thể vui lòng xem xét.

Một phần nơi tôi đang cập nhật với cổ phiếu bên trái:

value1 = int(self.stock) - int(self.qne.get()) 
sql = "UPDATE products SET stock = " + str(value1) + " WHERE pk = " + str(self.ident.get())
conn.execute(sql)
conn.close()

Mã đầy đủ của tôi cho đến bây giờ:

# import random
from tkinter import *
import datetime
import sqlite3

conn = sqlite3.connect('database.db')

products_list = []
quantity_list = []
price_list = []
class Application:
    def __init__(self, master):
        self.master = master
        # today's date
        date = datetime.datetime.now()
        # frames
        self.left = Frame(master, width=800, height=700, bg='white')
        self.left.pack(side=LEFT)

        self.right = Frame(master, width=400, height=700, bg='steelblue')
        self.right.pack(side=RIGHT)

        # heading
        self.heading = Label(self.left, text="Welcome to Store Management System", font=('arial 40 bold'), fg='steelblue', bg='white')
        self.heading.place(x=0, y=0)

        # cart
        self.cart = Label(self.right, text=("TEST TRADE LINK"), font=('arial 25 bold'), fg='white', bg='steelblue', padx=10, pady=10)
        self.cart.place(x=0, y=0)

        # datelabel
        self.datelabel = Label(self.right, text= (str(date)[:10]), font=('arial 25 bold'), fg='white', bg='steelblue')
        self.datelabel.place(x=0,y=55)

        # template for bill
        self.bpro = Label(self.right, text= "Products", font=('arial 16 bold'), fg='white', bg='steelblue')
        self.bpro.place(x=0, y=100)

        self.bqua = Label(self.right, text= "Quantity", font=('arial 16 bold'), fg='white', bg='steelblue')
        self.bqua.place(x=120, y=100)

        self.bpri = Label(self.right, text= "Price", font=('arial 16 bold'), fg='white', bg='steelblue')
        self.bpri.place(x=240, y=100)


        # label for the input
        self.id = Label(self.left, text="Enter Unique ID", font=('arial 16 bold'), bg='white')
        self.id.place(x=0, y=80)

        # entry for the unique id
        self.inp = IntVar()
        self.ident = Entry(self.left, width=50, bg='white', textvariable=self.inp)
        self.ident.place(x=190, y=82)

        # button
        self.submit = Button(self.left, text="Submit", width=17, height=2, command=self.ajax)
        self.submit.place(x=400, y=125)

        # ajax contents
        self.products_name = Label(self.left, text="", font=('arial 16 bold'), bg='white')
        self.products_name.place(x=0, y=250)

        self.price = Label(self.left, text="", font=('arial 16 bold'), bg='white')
        self.price.place(x=0, y=290)

        self.expiration = Label(self.left, text="", font=('arial 16 bold'), bg='white')
        self.expiration.place(x=0, y=330)

        # down reached upto y=440
        self.total = Label(self.left, text="", font=('arial 16 bold'), bg='white')
        self.total.place(x=0, y=480)
    def ajax(self):
        if self.ident.get() == "0":
            print("Failed")
        else:
            self.result = conn.execute("SELECT * FROM products WHERE pk LIKE ?", [str(self.ident.get())])
            for self.row in self.result:
                self.product = self.row[1]
                self.expd = self.row[4]
                self.cost = self.row[3]
                self.stock = self.row[2]
                self.products_name.configure(text="Product Name: " + str(self.product))
                self.price.configure(text="Price Per Unit: Rs. " + str(self.cost) + " \t")
                self.expiration.configure(text="Expiration Date: " + str(self.expd))

                # new labels and entries
                self.qn = Label(self.left, text="Quantity", font=('arial 16 bold'), bg='white')
                self.qn.place(x=0, y=370)

                self.q = IntVar()
                self.qne = Entry(self.left, width=50, bg='white', textvariable=self.q)
                self.qne.place(x=150, y=371)

                self.discount = Label(self.left, text="Discount", font=('arial 16 bold'), bg='white')
                self.discount.place(x=0, y=410)

                self.d = IntVar()
                self.dise = Entry(self.left, width=50, bg='white', textvariable=self.d)
                self.dise.place(x=150, y=410)

                self.btnadd = Button(self.left, width=17, height=2, text="Add to Cart", bg='lightblue', command=self.add_to_cart)
                self.btnadd.place(x=400, y=440)

                self.bill = Button(self.left, width=17, height=2, text='Generate Bill', bg='lightgreen', command=self.print_bill)
                self.bill.place(x=200, y=440)
    def add_to_cart(self):
        products_list.append(self.product)
        quantity_list.append(self.qne.get())
        price_list.append(int(self.qne.get()) * int(self.cost))

        value1 = int(self.stock) - int(self.qne.get()) 
        sql = "UPDATE products SET stock = " + str(value1) + " WHERE pk = " + str(self.ident.get())
        conn.execute(sql)
        conn.close()
        t = 150
        i = 0
        for p in products_list:
            self.pr1 = Label(self.right, text=str(products_list[i]), font=('arial 16 bold'), bg='steelblue', fg='white').place(x=0, y=t)
            self.pr2 = Label(self.right, text=(str(quantity_list[i])), font=('arial 16 bold'), bg='steelblue', fg='white').place(x=120, y=t)
            self.pr3 = Label(self.right, text=(str(price_list[i])), font=('arial 16 bold'), bg='steelblue', fg='white').place(x=240, y=t)
            t += 40
            i += 1

        print(str(products_list) + str(quantity_list)+ str(price_list))
    def print_bill(self):
        f = open("bill.txt", 'w+')
        f.write(str(products_list) + str(quantity_list) + str(price_list))
        f.close()

        # reset all after each purchase
        # delete the temporary cart
        del(products_list[:])
        del(quantity_list[:])
        del(price_list[:])

        # # delete the labels
        # self.pr1.configure(text="")
        # self.pr2.configure(text="")
        # self.pr3.configure(text="")
root = Tk()
b = Application(root)
# root.resizable(False, False)
root.geometry("1200x720+0+0")

root.mainloop()

Tôi đã có 50 cho sản phẩm đầu tiên và tôi đang cố gắng cập nhật nó, nhưng khi tôi kiểm tra nó sau khi bản cập nhật được thực thi, nó hiển thị 50.

Cơ sở dữ liệu của tôi:

Hướng dẫn python update query not working - truy vấn cập nhật python không hoạt động

Cú pháp để cập nhật là gì?

Cú pháp Cập nhật SQL cơ bản đi xuống bằng cách sử dụng Cập nhật từ khóa, theo sau là tên của đối tượng của chúng tôi (Bí danh bảng hoặc bảng) và tên cột được đặt bằng một số giá trị.Điều khoản từ sẽ xuất hiện khi chúng tôi tham gia và chúng tôi cũng có thể có một mệnh đề WHERE khi chúng tôi chỉ cần cập nhật một phần dữ liệu trong bảng.keyword UPDATE followed by the name of our object (table or table alias) and the SET column name equals to some values. The FROM clause will come into play when we do joins and we can also have a WHERE clause when we need to update only a portion of data in a table.

Tôi có thể sử dụng từ mệnh đề trong cập nhật không?

Cập nhật các câu lệnh với mệnh đề từ mệnh đề thường được sử dụng để cập nhật thông tin trong bảng dựa trên tham số có giá trị bảng (TVP) hoặc để cập nhật các cột trong bảng trong một kích hoạt sau.Đối với kịch bản cập nhật dựa trên TVP, hãy xem việc thực hiện chức năng hợp nhất trong quy trình được lưu trữ được biên dịch tự nhiên.. For the scenario of update based on a TVP, see Implementing MERGE Functionality in a Natively Compiled Stored Procedure.

Chúng tôi có thể sử dụng và vận hành trong cập nhật không?

SQL và điều kiện (còn được gọi là và toán tử) được sử dụng để kiểm tra hai hoặc nhiều điều kiện theo lựa chọn, chèn, cập nhật hoặc xóa câu lệnh.used to test for two or more conditions in a SELECT, INSERT, UPDATE, or DELETE statement.

Lệnh cập nhật trong SQL là gì?

Tuyên bố cập nhật được sử dụng để sửa đổi các bản ghi hiện có trong một bảng.used to modify the existing records in a table.