0

I am trying to figure out how can I get this to work and this is a side project I decided to make apart from the studies on my own. What I need is to be able to search items in a category column by providing a string like "Mobile" and then I return a list of items in a particular category, but that is working for me for another functions.

I hope this is sufficient.

def search_ims_category_items(category):
    with DatabaseConnection('inventory.db') as connection:
        cursor = connection.cursor()
        cursor.execute(f'SELECT * FROM inventory WHERE category = ?', (category,))

This is where the return should happen

def search_category_items():
    category = input("Enter the category name you want to find: ")
    items = database.search_ims_category_items(category)
    for item in items:
        print(
            f"{item['product_name']} | {item['product_number']} | {item['category']} | ${item['price']} | ${item['discount']} | {item['quantity']}")

Much appreciated!

What I do is I execute the query where category needs to be like input category name from other function. Tried with %s, and other methods, but I cannot get this to work. And, I have checked Python docs before coming here, but can't quite figure this out.

  • This should work. But your function never returns the result of the query. You need something like `return cursor.fetchall()` – Barmar Dec 13 '22 at 22:16
  • `category` isn't a comma-separated list, is it? See https://stackoverflow.com/questions/6979813/alternative-to-find-in-set-in-sqlite for how to search this. – Barmar Dec 13 '22 at 22:18
  • See https://stackoverflow.com/questions/3300464/how-can-i-get-dict-from-sqlite-query for how to configure `mysql-sqlite` to return dictionaries instead of tuples. – Barmar Dec 13 '22 at 22:21

0 Answers0