Error in parameterized query: TypeError: list indices must be integers, not str

1

I am developing a GUI that manages my database in Python 2.7 that connects to a MySQL database. You can already insert, consult and delete but what you can not do yet is update with the UPDATE statement. I get the following error:

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

Why do you throw that error ?. My code is this:

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)
    
asked by Erick Finn 11.10.2018 в 23:59
source

1 answer

1

The problem as shown by the error is in:

cursor.execute("UPDATE base17 SET %s='Erick' WHERE ID =%s",[d2][d1])

specifically when passing parameters of the parameterized query, [d2][d1] . The variables d1 and d2 refer both to chains that come from paths tkinter.StringVar , when you make [d2] you create a list that contains the string associated with the variable d2 :

>>> d2 = "Hola"
>>> [d2]
['Hola']

So [d2][d1] is interpreted as an indexing attempt on list [d2] using the variable d1 , which is a string, and therefore is not a valid index.

>>> ["hola"]["mundo"]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: list indices must be integers or slices, not str

You must simply pass a single tuple (or list) with the parameters of the query to be replaced:

cursor.execute("UPDATE base17 SET %s='Erick' WHERE ID =%s", (d2, d1))
  

Note: : if a tuple is used and there is only one parameter, remember to always add a comma after it, otherwise the parentheses will not indicate a tuple and we will have an error as a consequence. Instead of:

cursor.execute('SELECT * FROM foo WHERE bar = %d', (13))
     

should be:

cursor.execute('SELECT * FROM foo WHERE bar = %d', (13,))
    
answered by 12.10.2018 / 06:20
source