int() argument must be a string, a bytes-like object or a number, not 'tuple' will using sql formating in python
Question:
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:
x = [0, 1, 2]
y = ''.join(map(str, x))
z = int(y)
but to no avail.
Answers:
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])
Each query always produces a tuple of results, no matter how many fields the SELECT
statement requests, so that the API is not dependent on the particular argument to execute
.
Likewise, fetchall
returns a list of results, so that the API doesn’t change depending on whether the query produces 0, 1, or more results.
So you need to index cursor.fetchall()
twice: once to get the last (and only?) element of the result, and once to get the only value in tuple.
adding_item_ID = int(cursor.fetchall()[-1][0])
It’s possible you could refine the query to avoid needing fetchall
. Use something like
cursor.execute('SELECT max(item_ID) FROM Items_in_stock ORDER BY item_ID')
and then you can use
adding_item_ID = int(cursor.fetchone()[0])
This will be more efficient as well, as the work of selecting the largest item id is done in the database, not in Python, and only one result has to be retrieved.
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:
x = [0, 1, 2]
y = ''.join(map(str, x))
z = int(y)
but to no avail.
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])
Each query always produces a tuple of results, no matter how many fields the SELECT
statement requests, so that the API is not dependent on the particular argument to execute
.
Likewise, fetchall
returns a list of results, so that the API doesn’t change depending on whether the query produces 0, 1, or more results.
So you need to index cursor.fetchall()
twice: once to get the last (and only?) element of the result, and once to get the only value in tuple.
adding_item_ID = int(cursor.fetchall()[-1][0])
It’s possible you could refine the query to avoid needing fetchall
. Use something like
cursor.execute('SELECT max(item_ID) FROM Items_in_stock ORDER BY item_ID')
and then you can use
adding_item_ID = int(cursor.fetchone()[0])
This will be more efficient as well, as the work of selecting the largest item id is done in the database, not in Python, and only one result has to be retrieved.