Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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.

try:
    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
    print(adding_item_ID)
    print(adding_item_ID2)
    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(str(e))
    print("Unable to add item")

I have tried to do this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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])
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading