TypeError:列表索引必须是整数,而不是str-Python-MySQL

时间:2018-10-11 20:01:53

标签: mysql python-2.7 user-interface tkinter

我需要您的帮助,因为当我尝试更新MySQL数据库上的寄存器时,我不知道为什么会出现此错误。我有一个使用Python的Tkinter进行的GUI,可以创建,删除和查看数据库中的数据,但是当我尝试应用更新时,出现以下错误。我想做的是,用户可以说出他想在哪个字段中进行更新,要更新什么值以及最后在WHERE语句中的条件。

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "C:\Users\efrias002\Desktop\Python\SQL Tkinter v2.py", line 136, in 
cambiar
cursor.execute("UPDATE base17 SET %s='Erick' WHERE ID =%s",[d2][d1])
TypeError: list indices must be integers, not str

我正在使用Python 2.7,我的代码是这样:

def win2 ():
  t1 = Toplevel(bg="Brown")
  t1.title("Modificar Datos")
  t1.geometry('580x400')
  t1.focus_set()
  t1.grab_set()
  t1.transient(master=ventana)
  t1.resizable(width = FALSE, height = FALSE)


  lb1 = Label(t1,text='ID del Registro',bg="Cyan2")
  lb1.grid(row=5, column=1,padx=70,pady=10)

  inf2=StringVar()
  t2=Entry(t1,textvariable=inf2)
  t2.grid(row=6,column=1,pady=15)

  lb2 = Label(t1,text='Campo del Dato a Modificar',bg="Cyan2")
  lb2.grid(row=5, column=3,padx=25,pady=5)

  inf3=StringVar()
  t3=Entry(t1,textvariable=inf3)
  t3.grid(row=6,column=3,pady=15)

  lb3 = Label(t1,text='Dato a Modificar',bg="Cyan2")
  lb3.grid(row=11, column=2,padx=10,pady=10)

  inf4=StringVar()
  t4=Entry(t1,textvariable=inf4)
  t4.grid(row=12,column=2,pady=10)

  def cambiar():
         d1= inf2.get()
         d2= inf3.get()
         d3= inf4.get()
         conn = MySQLdb.connect(host = "127.0.0.1", user = "root", passwd = "unitec", db = "efrias002")
         cursor = conn.cursor()
         cursor.execute("UPDATE base17 SET %s='Erick' WHERE ID =%s",[d2][d1])
         conn.commit()
         conn.close()

         tkMessageBox.showinfo("Info","Registro Modificado")



  wb = Frame(t1, width = 15, height = 10)
  b1 = Button(wb, text = "Cerrar(X)", bg = "SkyBlue", command = t1.destroy)
  b1.pack()
  wb.grid(column = 1, row = 13, pady=85)

  wc = Frame(t1, width = 15, height = 10)
  b2 = Button(wc, text = "Enviar", bg = "Orange", command = cambiar)
  b2.pack()
  wc.grid(column = 2, row = 13, pady=35)

0 个答案:

没有答案