Если product
равно "stop"
, нет необходимости запрашивать количество или , чтобы добавить что-либо в список покупок. Сделайте эту проверку ранее .
while True:
try:
product = get_user_input("Input the product name: ")
if product == "stop":
break
quantity = get_user_input("Input the product quantity: ", int)
shopping_list.add_item(product, quantity)
except Exception as e:
print("\nAn error occurred:", e)
Чтобы узнать, сколько строк находится в наборе результатов, у вас есть ровно две опции:
select count (*)
Вы можете ввести некоторую магию с помощью хранимой процедуры, которая возвращает массив или что-то более интересное, но в конечном итоге должна произойти одна из этих двух вещей.
Итак, fancypants способа получить такой результат не существует. Их просто надо посчитать: -)
На основании беглого взгляда здесь , кажется, что после запуска
$statement->execute($arg)
вы можете получить доступ к количеству строк через
$statement->rows
The "caveat" in the documentation (linked to in a comment on another answer) is important, and provides the real, correct answer:
Generally, you can only rely on a row count after a non-SELECT execute (for some specific operations like UPDATE and DELETE), or after fetching all the rows of a SELECT statement.
For SELECT statements, it is generally not possible to know how many rows will be returned except by fetching them all. Some drivers will return the number of rows the application has fetched so far, but others may return -1 until all rows have been fetched. So use of the rows method or $DBI::rows with SELECT statements is not recommended.
Строки определенно различаются в зависимости от версии базы данных / драйвера. Я бы определенно поискал там логику, которая работала бы с неожиданными результатами.