Search code examples
pythonmysqltuples

'tuple' object does not support item assignment on array


I want to get select from my db ,when I tried to change one of the cells

here is my code:

command = "select desc ,city,datetime,loc from mytable'"
cursor.execute(command)
result = cursor.fetchall()
i = 0
for x in result:
   myary.append(result[i])
   i= i+1

my_list = list(myary)
for y in range(0,len(myary)):
    sip = myary[y][0].split("/")
    my_list [y][0]=sip[0]
myary = tuple(my_list)

Output:

'tuple' object does not support item assignment

Can anyone please tell me what's wrong?


Solution

  • tuples are immutable in python. In the code you're trying to modify one. To fix it you can convert it to a list. Something like:

    command = "select desc ,city,datetime,loc from mytable'"
    cursor.execute(command)
    result = cursor.fetchall()
    
    my_list = [list(x) for x in result]  # Convert each tuple to a list
    
    for y in range(0,len(my_list)):
        sip = my_list[y][0].split("/")
        my_list[y][0]=sip[0]
    
    myary = tuple(my_list)  # Convert them back to a tuple of tuples