试图同时将用户输入存储到数组和数据库中

2024-04-26 11:06:08 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图同时将用户输入存储到数组和数据库中,但是我只能将用户输入存储到数据库中,而不能存储到数组中

这是我的密码:

from tkinter import *
from tkinter import messagebox
from PIL import ImageTk
import cont
import pymysql


class Venues:
    def __init__(self, num, seatCap):
        self._num = num
        self._seatCap = seatCap

    def get_num(self):
        return self._num

    def get_seatCap(self):
        return self._seatCap


class Create:
    def __init__(self,root):
        self.root = root

        bg = Label(self.root, text="This is an image (Images/bgimage.jpg)").place(x=0, y=0, relwidth=1, relheight=1)

        framecreate = Frame(self.root, bg="white")
        framecreate.place(x=450, y=100, height=500, width=700)

        venuecreate = Label(framecreate, text="Please add the venue").place(x=50,y=100)
        self._num = Entry(framecreate)
        self._num.place(x=50, y=130, width=250)
        capacitycreate = Label(framecreate, text="Please add the capacity of the venue").place(x=370,y=100)
        self._seatCap = Entry(framecreate)
        self._seatCap.place(x=370, y=130, width=250)
        timecreate = Label(framecreate, text="Please add the time").place(x=50,y=170)
        self.time = Entry(framecreate)
        self.time.place(x=50, y=200, width=250)
        lecturercreate = Label(framecreate, text="Please add the lecturer").place(x=370,y=170)
        self.lecturer = Entry(framecreate)
        self.lecturer.place(x=370, y=200, width=250)
        modulercreate = Label(framecreate, text="Please add the module").place(x=50,y=240)
        self.subject = Entry(framecreate)
        self.subject.place(x=50, y=270, width=250)
        departmentcreate = Label(framecreate, text="Please add the department").place(x=370,y=240)
        self.department = Entry(framecreate)
        self.department.place(x=370, y=270, width=250)
        addbutton = Button(framecreate, text="Add", command=self.adddetails)
        addbutton.place(x=50, y=350)
        nextbutton = Button(framecreate, text="Next", command=self.cont)
        nextbutton.place(x=50, y=400)

    def adddetails(self):
        try:
            con = pymysql.connect(host="localhost",user="root",password="",database="timetable")
            cur = con.cursor()
            cur.execute("insert into timetablelist (venue,venuepax,time,lecturer,module,department) values(%s,%s,%s,%s,%s,%s)",
                        (self._num.get(),self._seatCap.get(),self.time.get(),self.lecturer.get(),self.subject.get(),self.department.get()))
            con.commit()
            con.close()
            messagebox.showinfo("Success","Details Added to Timetable",parent=self.root)
            #i have defined an array here for the venue and the venuepax that has been entered by the user
            self.venue = []
            for i in range(0, len(self.venue)):
                self.venue.append(Venues(self.venue[i], self.venue[i]))
            print(self.venue)
        except Exception as ex:
            messagebox.showerror("Error", f"Error due to: {str(ex)}", parent=self.root)

根据代码,我可以将用户详细信息插入数据库,但是当我打印数组时,它只显示一个空数组


Tags: thetextselfaddgetplacerootwidth
1条回答
网友
1楼 · 发布于 2024-04-26 11:06:08

说明:

您的错误是,每次运行函数时,列表都将设置为空列表,因此,由于列表为空,len(list)将为0,因此循环将运行0次(无效),而列表将保持为空

解决方案:

您可以在这里做的是,首先将列表移动到__init__中,以便:

def __init__(self,root):
    # Rest of code...
    self.venue = []
    # Rest of code...

现在有两种方法可以继续,从数据库中获取最近添加的值并附加到列表中,或者可以将相同的值元组插入列表中

这里,我将向您展示如何插入数据库,然后插入列表,因为其他方法需要if条件和更多条件

def adddetails(self):
    try:
        con = pymysql.connect(host="localhost",user="root",password="",database="timetable")
        cur = con.cursor()

        values = (self._num.get(),self._seatCap.get(),self.time.get(),self.lecturer.get(),self.subject.get(),self.department.get())
        cur.execute("insert into timetablelist (venue,venuepax,time,lecturer,module,department) values(%s,%s,%s,%s,%s,%s)",values)
        con.commit()
        con.close()
        messagebox.showinfo("Success","Details Added to Timetable",parent=self.root)
        
        self.avenue.append(values)
        # Rest of code...

因此,您的列表将采用以下形式:

[(n1,n2,n3,...),(a1,a2,a3,...),..] # print(self.avenue)

相关问题 更多 >