int() argument must be a string, a bytes-like object or a number, not 'tuple' will using sql formating in python


I am making a code to add an item to a database, however, I get the error

int() argument must be a string, a bytes-like object or a number, not 'tuple'

as I am trying to turn the information you get from a tuple when using the fetchall() function in python (i.e. (1,),(2,),(3,) into an int, however I do not know how to do this. Here is the code I am working on.

    adding_item_name = input("What would you like to name the item to be? >>> ")
    adding_item_value = input("What would you like the value of the item to be? >>> ")
    adding_item_quantity = int(input("What would you like the quantity to be? >>> "))
    cursor.execute('SELECT item_ID FROM Items_in_stock ORDER BY item_ID')
    adding_item_ID = int(cursor.fetchall()[-1])
    adding_item_ID += 1
    cursor.execute('INSERT INTO Items_in_stock (item_ID, item_name, item_value, item_quantity) VALUES (?,?,?,?)',
    (adding_item_ID_int, adding_item_name, adding_item_value, adding_item_quantity,))
    print("Added item successfully")
except Exception as e:
    print("Unable to add item")

I have tried to do this:

x = [0, 1, 2]
y = ''.join(map(str, x))
z = int(y)

but to no avail.

>Solution :

you can try this:

adding_item_ID = cursor.fetchall()[-1][0]  # get the first element of the tuple
adding_item_ID = int(adding_item_ID)  # convert to int

you can also use a for loop to iterate over the tuple and convert each element to an integer:

for item_id in cursor.fetchall():
    item_id = int(item_id[0])

Leave a Reply Cancel reply