【问题标题】:how to appear data with mysql and tkinter when I click search buttonhow to appear data with mysql and tkinter when I click search button
【发布时间】:2022-12-01 23:09:56
【问题描述】:

I'm making the data stored in mysql workbench come out when I click the search button by clicking one in the dataList, but as shown in the picture below, only the 'Plants' table appears, the 'PlantsMemo' data does not appear, and an error pops up.

enter image description here

the Error is "for RH, LM, TEMP, Tree, Flower, Fruit, min_height, max_height, avg_height, memo_info in allplantmemo:

ValueError: too many values to unpack (expected 10)"

I divided into mysql class and tkinter class

Below is the code I wrote

class final()

# create mysql table code(class final)
    def createTables(self):
        conn, cursor = self.connect()
        self.useGuiDB(cursor)
        cursor.execute("CREATE TABLE Plants( \
                        Plant_ID INT AUTO_INCREMENT, \
                        date VARCHAR(25) NOT NULL, \
                        time VARCHAR(25) NOT NULL, \
                        recorder VARCHAR(25) NOT NULL, \
                        location VARCHAR(25), \
                        count INT, \
                        seed_name VARCHAR(25) NOT NULL, \
                        PRIMARY KEY (Plant_ID) \
                    )  ENGINE=InnoDB")
        cursor.execute("CREATE TABLE PlantsMemo( \
                        PlantMemo_ID INT AUTO_INCREMENT, \
                        RH INT, \
                        LM INT, \
                        TEMP INT, \
                        Tree INT, \
                        Flower INT, \
                        Fruit INT, \
                        min_hieght INT,\
                        max_height INT, \
                        avg_height INT, \
                        memo_info VARCHAR(255), \
                        dataList INT, \
                        PRIMARY KEY (PlantMemo_ID), \
                            FOREIGN KEY (dataList) \
                            REFERENCES Plants(Plant_ID) \
                        ON DELETE CASCADE    \
                    )  ENGINE=InnoDB")

  # class final
    def searchData(self, ID):
        conn, cursor = self.connect()
        self.useGuiDB(cursor)

        cursor.execute("SELECT * FROM Plants where Plant_ID = ({})".format(ID))
        allPlants_1 = cursor.fetchall()
        cursor.execute("SELECT * FROM PlantsMemo")
        allPlants_2 = cursor.fetchall()

        return allPlants_1, allPlants_2

class Program()

# class program
    def insert(self, ID):
        allplant, allplantmemo = self.final.searchData(ID)
        for id_1, date, time, recorder, location, count, seed_name in allplant:
            str(id_1)
            self.date_Entry.insert(END, date)
            self.time_cobbx.insert(END,time)
            self.recorder_Entry.insert(END,recorder)
            self.RadioVar.set(location)
            self.count_combx.insert(END,count)
            self.seed_Entry.insert(END,seed_name)

        for RH, LM, TEMP, Tree, Flower, Fruit, min_height, max_height, avg_height, memo_info in allplantmemo:
            self.RH_spinbx.insert(END, RH)
            self.LM_spinbx.insert(END, LM)
            self.TEMP_spinbx.insert(END, TEMP)
            self.tree_spinbx.insert(END, Tree)
            self.flower_spinbx.insert(END, Flower)
            self.fruit_spinbx.insert(END, Fruit)
            self.min_height_spinbx.insert(END, min_height)
            self.max_height_spinbx.insert(END, max_height)
            self.avg_height_spinbx.insert(END, avg_height)
            self.Memo_txt.insert(END, memo_info)

# search button
    def search(self):
        ID = self.List_txt.get(ACTIVE)
        arr = ID[-1]
        self.clear()
        self.insert(arr)

I want to appear the data from the 'Plants' table not only but so the 'PlantsMemo' table appear.

【问题讨论】:

  • The table PlantsMemo has 12 columns, but you just provide 10 variables in the line for RM, LM, ... in allplantmemo. Missing variables for PlantMemo_ID and dataList columns.

标签: mysql tkinter mysql-workbench


【解决方案1】:

The exception obviously tells you that the number of provided variables (10) does not match the number of columns (12) returned from table PlantsMemo.

You can either provide the 10 column names in the SQL inside searchData():

cursor.execute("SELECT RH, LM, TEMP, Tree, Flower, Fruit, min_height, max_height, avg_height, memo_info FROM PlantsMemo")

or provide the required number of variables in the for line inside insert():

for PlantMemo_ID, RH, LM, TEMP, Tree, Flower, Fruit, min_height, max_height, avg_height, memo_info, dataList in allplantmemo:
    ...

【讨论】:

    猜你喜欢
    • 2022-12-27
    • 2022-12-01
    • 2022-12-02
    • 1970-01-01
    • 2022-12-27
    • 1970-01-01
    • 2022-12-02
    • 2022-12-27
    • 2022-12-27
    相关资源
    最近更新 更多