Python存储在sqlite中动态创建的条目框

时间:2018-03-08 16:17:06

标签: python dynamic tkinter sqlite sql-update

我有这段代码,用于存储有关用户帐户的信息。每个帐户在数据库中都有自己的记录。每条记录中都有一个字段存储变量Exercises。当每个用户执行多个练习时,这需要是不同值的列表。要做到这一点,我创建了一个按钮,单击该按钮创建另一个输入框以输入练习。然而,由于这个条目列表都是相同的名称,我在存储每个框中保存的数据时遇到问题。这是代码

    global MemberID
    intMemberID = MemberID
    self.ent_MemberID = Entry(self.FrameExercise, bg = "PaleTurquoise1", font =("Arial","16"),  width = 20, text = MemberID)
    self.ent_MemberID.grid(row = 11, column = 12)
    self.ent_MemberID.insert(END, intMemberID) #no "\n"

    global ExerciseCount
    ExerciseCount = 0

    global WeightCount
    WeightCount = 0

    connection = sqlite3.connect(r"F:\Program\Accounts.db")
    cursor = connection.cursor()
    cursor.execute("SELECT * FROM Exercises WHERE MemberID=?", (MemberID,))#comma

def new(self):
    global ExerciseCount
    ExerciseCount = ExerciseCount + 1
    print (ExerciseCount)
    for num in range(ExerciseCount):
        self.Exercises = Entry(self.FrameExercise, bg = "PaleTurquoise1", font =("Arial","16"),  width = 20)
        self.Exercises.grid(row=2+ExerciseCount, column=1)

    global WeightCount
    WeightCount = WeightCount + 1
    print (WeightCount)
    for num in range(WeightCount):
        self.Weights = Entry(self.FrameExercise, bg = "PaleTurquoise1", font =("Arial","16"),  width = 4)
        self.Weights.grid(row=2+WeightCount, column=2)

def Update(self):

    connection = sqlite3.connect(r"F:\Program\Accounts.db")
    cursor = connection.cursor()

    Exercise = self.Exercises.get()
    Weight = self.Weights.get()
    global MemberID

    List = [Exercise, Weight, MemberID]

    cursor.executemany("UPDATE Exercises SET Exercise=?, Weight=? WHERE MemberID=?",(Exercise, Weight, MemberID))

    connection.commit()

当我创建2个输入框时,这是错误:

sqlite3.ProgrammingError: Incorrect number of bindings supplied. The current statement uses 3, and there are 1 supplied

1 个答案:

答案 0 :(得分:0)

exercise应该存储在一个单独的exercise表中,该表与member绑定MemberID上的外键关系。然后根据需要编写尽可能多的exercisemember