我下载了python简单清单系统 它具有3列:产品名称,产品数量和产品价格 我仔细检查并在程序中其他三个产品名称,产品数量和产品价格处添加了产品位置。 现在,当它运行时,我得到此错误: sqlite3.OperationalError:表产品没有名为product_location的列
有人建议从(如果不存在则创建表)中删除“如果不存在”,但是这样做不起作用。
PASSWORD = StringVar()
PRODUCT_NAME = StringVar()
PRODUCT_PRICE = IntVar()
PRODUCT_QTY = IntVar()
PRODUCT_LOCATION = IntVar()
SEARCH = StringVar()```
I downloaded python simple inventory system from ```https://www.sourcecodester.com/tutorials/python/11417/python-simple-inventory-system.html.```
It had 3 columns: product name, product quantity and product price.
I want to use at work but I need product location as well. I went through and added product location everywhere the other 3 were in the program. There were some other things I changed but don’t recall all of them.
Now when it runs I get this error:
```sqlite3.OperationalError: table product has no column named product_location.```
I have looked at this page ``https://stackoverflow.com/questions/21881992/when-is-sqliteopenhelper-oncreate-onupgrade-run`` which has a lot about this problem but it is over my head. I don’t usually work with code nearly this big. Wondering if someone could give me an idea what to try that I may understand.
Someone suggested remove if not exist from create table but that did not work
def Database():
global conn, cursor
conn = sqlite3.connect("pythontut.db")
cursor = conn.cursor()
cursor.execute("CREATE TABLE IF NOT EXISTS `admin` (admin_id INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL, username TEXT, password TEXT)")
cursor.execute("CREATE TABLE IF NOT EXISTS `product` (product_id INTEGER
PRIMARY KEY AUTOINCREMENT NOT NULL, product_name TEXT, product_qty TEXT,
product_price TEXT)")
cursor.execute("SELECT * FROM `admin` WHERE `username` = 'admin' AND
`password` = 'admin'")
if cursor.fetchone() is None:
cursor.execute("INSERT INTO `admin` (username, password)
VALUES('admin', 'admin')")
conn.commit()
def AddNewForm():
TopAddNew = Frame(addnewform, width=600, height=100, bd=1, relief=SOLID)
TopAddNew.pack(side=TOP, pady=20)
lbl_text = Label(TopAddNew, text="Add New Product", font=('arial', 18),
width=600)
lbl_text.pack(fill=X)
MidAddNew = Frame(addnewform, width=600)
MidAddNew.pack(side=TOP, pady=50)
lbl_productname = Label(MidAddNew, text="Product Name:", font=('arial',
25), bd=10)
lbl_productname.grid(row=0, sticky=W)
lbl_qty = Label(MidAddNew, text="Product Quantity:", font=('arial', 25),
bd=10)
lbl_qty.grid(row=1, sticky=W)
lbl_price = Label(MidAddNew, text="Product Price:", font=('arial', 25),
bd=10)
lbl_price.grid(row=2, sticky=W)
lbl_productlocation = Label(MidAddNew, text="Product Location:", font=
('arial', 25), bd=10)
lbl_productlocation.grid(row=3, sticky=W)
productname = Entry(MidAddNew, textvariable=PRODUCT_NAME, font=('arial',
25), width=15)
productname.grid(row=0, column=1)
productqty = Entry(MidAddNew, textvariable=PRODUCT_QTY, font=('arial',
25), width=15)
productqty.grid(row=1, column=1)
productprice = Entry(MidAddNew, textvariable=PRODUCT_PRICE, font=
('arial', 25), width=15)
productprice.grid(row=2, column=1)
productlocaton = Entry(MidAddNew, textvariable=PRODUCT_LOCATION, font=
('arial', 25), width=15)
btn_add = Button(MidAddNew, text="Save", font=('arial', 18), width=30,
bg="#009ACD", command=AddNew)
btn_add.grid(row=4, columnspan=2, pady=20)
def ViewForm():
global tree
TopViewForm = Frame(viewform, width=600, bd=1, relief=SOLID)
TopViewForm.pack(side=TOP, fill=X)
LeftViewForm = Frame(viewform, width=600)
LeftViewForm.pack(side=LEFT, fill=Y)
MidViewForm = Frame(viewform, width=600)
MidViewForm.pack(side=RIGHT)
lbl_text = Label(TopViewForm, text="View Products", font=('arial', 18),
width=600)
lbl_text.pack(fill=X)
lbl_txtsearch = Label(LeftViewForm, text="Search", font=('arial', 15))
lbl_txtsearch.pack(side=TOP, anchor=W)
search = Entry(LeftViewForm, textvariable=SEARCH, font=('arial', 15),
width=10)
search.pack(side=TOP, padx=10, fill=X)
btn_search = Button(LeftViewForm, text="Search", command=Search)
btn_search.pack(side=TOP, padx=10, pady=10, fill=X)
btn_reset = Button(LeftViewForm, text="Reset", command=Reset)
btn_reset.pack(side=TOP, padx=10, pady=10, fill=X)
btn_delete = Button(LeftViewForm, text="Delete", command=Delete)
btn_delete.pack(side=TOP, padx=10, pady=10, fill=X)
scrollbarx = Scrollbar(MidViewForm, orient=HORIZONTAL)
scrollbary = Scrollbar(MidViewForm, orient=VERTICAL)
tree = ttk.Treeview(MidViewForm, columns=("ProductID", "Product Name",
"Product Qty", "Product Price", "Product Location"),
selectmode="extended", height=100, yscrollcommand=scrollbary.set,
xscrollcommand=scrollbarx.set)
scrollbary.config(command=tree.yview)
scrollbary.pack(side=RIGHT, fill=Y)
scrollbarx.config(command=tree.xview)
scrollbarx.pack(side=BOTTOM, fill=X)
tree.heading('ProductID', text="ProductID",anchor=W)
tree.heading('Product Name', text="Product Name",anchor=W)
tree.heading('Product Qty', text="Product Qty",anchor=W)
tree.heading('Product Price', text="Product Price",anchor=W)
tree.column('#0', stretch=NO, minwidth=0, width=0)
tree.column('#1', stretch=NO, minwidth=0, width=0)
tree.column('#2', stretch=NO, minwidth=0, width=200)
tree.column('#3', stretch=NO, minwidth=0, width=120)
tree.column('#4', stretch=NO, minwidth=0, width=120)
tree.pack()
DisplayData()
答案 0 :(得分:0)
我通过将conn = sqlite3.connect(“ pythontut.db”)更改为 conn = sqlite3.connect(“ user.db”)