Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 试图同时将用户输入存储到数组和数据库中_Python_Arrays_Database_Tkinter - Fatal编程技术网

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

Python 试图同时将用户输入存储到数组和数据库中,python,arrays,database,tkinter,Python,Arrays,Database,Tkinter,我试图同时将用户输入存储到数组和数据库中,但是我只能将用户输入存储到数据库中,而不能存储到数组中 这是我的密码: 从tkinter导入* 从tkinter导入消息框 从PIL导入ImageTk 进口控制 导入pymysql 上课地点: 定义初始化(self、num、seatCap): self.\u num=num self.\u seatCap=seatCap def get_num(自我): 返回自我。\u num def get_seatCap(自身): 返回自我。\u seatCap 类

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

这是我的密码:

从tkinter导入*
从tkinter导入消息框
从PIL导入ImageTk
进口控制
导入pymysql
上课地点:
定义初始化(self、num、seatCap):
self.\u num=num
self.\u seatCap=seatCap
def get_num(自我):
返回自我。\u num
def get_seatCap(自身):
返回自我。\u seatCap
类创建:
定义初始化(自,根):
self.root=根
bg=Label(self.root,text=“这是一个图像(Images/bgimage.jpg)”).place(x=0,y=0,relwidth=1,relheight=1)
framecreate=Frame(self.root,bg=“白色”)
framecreate.place(x=450,y=100,高度=500,宽度=700)
venuecreate=标签(framecreate,text=“请添加场地”)。地点(x=50,y=100)
self.\u num=条目(framecreate)
自身数量位置(x=50,y=130,宽度=250)
capacitycreate=标签(framecreate,text=“请添加场馆的容量”)。地点(x=370,y=100)
self.\u seatCap=条目(framecreate)
自身位置(x=370,y=130,宽度=250)
timecreate=Label(framecreate,text=“请添加时间”).place(x=50,y=170)
self.time=条目(framecreate)
自我时间地点(x=50,y=200,宽度=250)
讲师创建=标签(framecreate,text=“请添加讲师”).place(x=370,y=170)
self.讲师=条目(framecreate)
自我讲师位置(x=370,y=200,宽度=250)
modulercreate=Label(framecreate,text=“请添加模块”).place(x=50,y=240)
self.subject=条目(framecreate)
自我主体位置(x=50,y=270,宽度=250)
departmentcreate=Label(framecreate,text=“请添加部门”).place(x=370,y=240)
self.department=条目(framecreate)
自身部门位置(x=370,y=270,宽度=250)
addbutton=按钮(framecreate,text=“Add”,command=self.adddetails)
添加按钮位置(x=50,y=350)
nextbutton=Button(framecreate,text=“Next”,command=self.cont)
下一个按钮位置(x=50,y=400)
def添加详细信息(自我):
尝试:
con=pymysql.connect(host=“localhost”,user=“root”,password=“”,database=“timeline”)
cur=con.cursor()
当前执行(“将值(%s,%s,%s,%s,%s,%s,%s,%s插入时间表列表(场馆、维纽帕克斯、时间、讲师、模块、部门)”,
(self.\u num.get(),self.\u seatCap.get(),self.time.get(),self.讲师.get(),self.subject.get(),self.department.get())
con.commit()
con.close()
showinfo(“成功”,“详细信息添加到时间表”,parent=self.root)
#我在这里为用户输入的场馆和venuepax定义了一个数组
self.vice=[]
对于范围内的i(0,len(self.vention)):
self.vince.append(场馆(self.vince[i],self.vince[i]))
印刷品(自选场地)
例外情况除外,例如:
messagebox.batherror(“Error”,f“由于:{str(ex)}而导致的错误”,parent=self.root)
从代码开始,我可以将用户详细信息插入数据库,但是当我打印数组时,它只显示一个空数组。

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

解决方案: 您可以在这里做的是,首先将列表移动到
\uuuu init\uuuu
中,以便:

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)

当你打印。。。什么阵列?如果您谈论的是self.venture,那么您正在将其初始化为空,尝试对其进行迭代以向同一数组中添加内容。这毫无意义。@AKX我正试图将用户输入添加到数组中,但我发现很难执行soFair。如果您查看数组的代码,它对您有什么意义?它不引用任何用户输入。@AKX您能建议一种方法,我可以将用户输入同时添加到数组和数据库中吗?您会如何处理数组?如果它应该显示数据库中的所有时间表条目,那么就不需要直接向其中添加用户输入;只需从数据库中读回数据。