我不断收到错误消息:
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Users\max\AppData\Local\Programs\Python\Python37\lib\tkinter\__init__.py", line 1705, in __call__
return self.func(*args)
File "G:\computing project\add to db.py", line 114, in get_items
C.execute(sql ,vari)
sqlite3.OperationalError: near ")": syntax error
当尝试使用带有tkinter的gui输入的值添加到数据库时。我认为sql无法从输入中获取值。我试图将插入值更改为数据库中名称的值,但是python不允许theese包含大写字母。
我无法在代码中的任何地方找到错误中指出的“)”,因为这是我在发现语法错误时的第一个想法。我在添加到此数据库时遇到了一些问题,并且正在将sqlite 3与数据库浏览器一起使用。香港专业教育学院一直在遵循python教程,但我相信该教程使用的是python 2而不是3
from tkinter import*
import sqlite3
conn = sqlite3.connect("G:\computing project\database of cars.db")
C = conn.cursor()
import tkinter.messagebox
class Database:
def __init__ (self, master=None ,*args,**kwargs):
self.master = master
self.heading = Label(master , text = "add to the database ", font=("ariel 35 bold") )
self.heading.place (x=250 , y=0)
self.name= Label(master, text= "enter product name", font=("arial 18 bold"))
self.name.place(x=0,y=50)
self.make_1= Label(master , text = "enter car make ", font=("ariel 18 bold"))
self.make_1.place(x=0,y=100)
self.model_1= Label(master , text = "enter car model ", font=("ariel 18 bold"),)
self.model_1.place(x=0,y=150)
self.regi_1= Label(master , text = "enter registration plate", font=("ariel 18 bold"))
self.regi_1.place(x=0,y=200)
self.colour_1= Label(master , text = "enter car colour ", font=("ariel 18 bold"))
self.colour_1.place(x=0,y=250)
self.cost_1= Label(master , text = "enter cost price ", font=("ariel 18 bold"))
self.cost_1.place(x=0,y=300)
self.tcost_1= Label(master , text = "enter total cost price ", font=("ariel 18 bold"))
self.tcost_1.place(x=0,y=350)
self.sell_1= Label(master , text = "selling price ", font=("ariel 18 bold"))
self.sell_1.place(x=0,y=400)
self.tsell_1= Label(master , text = "enter total selling price", font=("ariel 18 bold"))
self.tsell_1.place(x=0,y=450)
self.assprof_1= Label(master , text = "enter assumed profit", font=("ariel 18 bold"))
self.assprof_1.place(x=0,y=500)
self.name_e = Entry(master, width=25, font=("arial 18 bold"))
self.name_e.place(x=300, y=50)
self.carmake_e = Entry(master, width=25, font=("arial 18 bold"))
self.carmake_e.place(x=300, y=100)
self.carmodel_e = Entry(master, width=25, font=("arial 18 bold"))
self.name_e.place(x=300, y=150)
self.regi_e = Entry(master, width=25, font=("arial 18 bold"))
self.regi_e.place(x=300, y=200)
self.colour_e = Entry(master, width=25, font=("arial 18 bold"))
self.colour_e.place(x=300, y=250)
self.carprice_e = Entry(master, width=25, font=("arial 18 bold"))
self.carprice_e.place(x=300, y=300)
self.cost_e = Entry(master, width=25, font=("arial 18 bold"))
self.cost_e.place(x=300, y=350)
self.tcost_e = Entry(master, width=25, font=("arial 18 bold"))
self.tcost_e.place(x=300, y=400)
self.sellprice_e = Entry(master, width=25, font=("arial 18 bold",))
self.sellprice_e.place(x=300, y=450)
self.assumedprofit_e = Entry(master, width=25, font=("arial 18 bold"))
self.assumedprofit_e.place(x=300, y=500)
self.b = Button(command=self.get_items)
self.b.place(x=0,y=0)
def get_items(self,*args,**kwargs): #this function gets the items from the entry boxes
self.carmake= self.carmake_e.get()
self.carmodel= self.carmodel_e.get()
self.regi= self.regi_e.get()
self.colour= self.colour_e.get()
self.cost= self.cost_e.get()
self.tcost= self.tcost_e.get()
self.sellprice= self.sellprice_e.get()
self.assumedprofit= self.assumedprofit_e.get()
self.assumedprofit= float(self.sellprice)- float(self.tcost)
if self.carmake == '' or self.carmodel == '' == self.colour == '':
print ("WRONG")
tkinter.messagebox.showinfo("error","please enter values for car make, model and colour")
else:
print("solid m8 ")
sql = "INSERT INTO inventory (carmake,carmode,regi,colour,cost,tcost,sellprice,assumedprofit) VALUES (?,?,?,?,?,?,?,?,)"
vari=(self.carmake,self.carmodel,self.regi,self.colour,self.cost,self.tcost,self.sellprice,self.assumedprofit)
C.execute(sql ,vari)
# C.execute(sql(self.name,self.carmake,self.carmodel,self.regi,self.colour,self.cost,self.tcost,self.sellprice,self.assumedprofit))
conn.commit()
tkinter.messagebox.showinfo("success","succesfully added to databse")
root = Tk()
b = Database(root)
frame = Frame(root,width=1920,height=1080)
root.geometry("1920x1080")
root.title("add to database")
root.configure(background="#b7b7e5")
root.mainloop()