0

i am trying to select an item from DB table using variable from optionmenu:

        c.execute("SELECT category_name FROM categories")
        llist = c.fetchall()        

        add_item_quan = Label(add_item_frame, text='الكمية ', bg='#d3e4ec', fg='#4422ee')
        add_item_quan.config(font='Aerial 12 bold')
        add_item_quan.grid(row=2, column=2, pady=10, padx=10)    

        item_cat = StringVar()
        add_item_cat_list_name = OptionMenu(add_item_frame, item_cat, *llist)
        add_item_cat_list_name.config(width=50, fg='white', bg='#4422ee')
        add_item_cat_list_name.grid(row=4, column=1)


        def add_new_item():
            c.execute("SELECT id FROM categories WHERE category_name = (?)", (item_cat, ))
            item_cat_id = c.fetchall()
            c.execute("SELECT category_name FROM categories WHERE id = (?) ", (item_cat_id, ))
            item_categ = c.fetchall()
            item_name = itemsname.get()
            itemsquan = int(itemsquan1)
            item_quant = itemsquan
            c.execute("INSERT INTO items (item_name, quan, item_category VALUES (?, ?, ?)",
                      (item_name, item_quant, item_categ, ))

        add_item_button = Button(add_item_frame, text='مــوافــق', fg='white', bg='#4422ee',                          command=add_new_item)
        add_item_button.grid(row=1, pady=10, padx=10)

every time it gives me the error:

Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Python27\lib\lib-tk\Tkinter.py", line 1541, in __call__
return self.func(*args)
File "G:/Python/warehouse/main.py", line 126, in add_new_item
c.execute("SELECT category_name FROM categories WHERE id = (?) ", 
(item_cat_id, ))
InterfaceError: Error binding parameter 0 - probably unsupported type.

the data base takes text and integers as data type:

CREATE TABLE `items` (
`id`    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT UNIQUE,
`item_name` TEXT NOT NULL,
`quan`  INTEGER NOT NULL,
`item_category` TEXT
);

Other solutions were telling to change the text types in every var to suit the DB but it didn't work, I tried it as :

        def add_new_item():
        c.execute("SELECT id FROM categories WHERE category_name = (?)", (str(item_cat), ))
        item_cat_id = c.fetchall()
        c.execute("SELECT category_name FROM categories WHERE id = (?) ", (int(item_cat_id), ))
        item_categ = c.fetchall()
        item_name = itemsname.get()
        itemsquan = int(itemsquan1)
        item_quant = itemsquan
        c.execute("INSERT INTO items (item_name, quan, item_category VALUES (?, ?, ?)",
                  (str(item_name), int(item_quant), str(item_categ), ))

And then it gave me the same error again

1
  • done, please check the updated part of the question Commented Mar 15, 2018 at 18:07

1 Answer 1

1

item_cat_id is the result of the first fetchall() and is therefore a list of tuples. Sqlite has no idea what to do with that.

You could use fetchone() and indexing to get the actual ID and pass that in instead:

c.execute("SELECT id FROM categories WHERE category_name = (?)", (item_cat, ))
item_cat_id = c.fetchone()[0]

However, it's hard to understand why you are doing these queries at all. You first ask the ID of the category with a specific name, then you ask for the name of the category with that returned ID. Not surprisingly, you will get back the name you started with, which seems a bit pointless.

Sign up to request clarification or add additional context in comments.

2 Comments

Frist thanks for ur answer,,,the reason am doing this that the category name is editable in the user interface and i want to use the id to bind the name to another table so that whenever they change the category name it doesn't affect the other table ,specially in reports
None of that makes sense. item_categ, once you've sorted out the type issues, will be exactly the same as item_cat.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.