InterfaceError:错误绑定参数0可能不受支持的类型

时间:2018-03-15 17:58:29

标签: python tkinter sqlite

我正在尝试使用optionmenu中的变量从DB表中选择一个项目:

        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)

每次它都给我错误:

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.

数据库将文本和整数作为数据类型:

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

其他解决方案告诉我们要更改每个var中的文本类型以适应数据库,但它不起作用,我试过它:

        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), ))

然后它又给了我同样的错误

1 个答案:

答案 0 :(得分:1)

item_cat_id是第一个fetchall()的结果,因此是元组列表。 Sqlite不知道该怎么做。

您可以使用fetchone()和索引来获取实际ID并将其传递给:

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

但是,很难理解为什么要进行这些查询。您首先询问具有特定名称的类别的ID,然后询问具有该返回ID的类别的名称。毫不奇怪,你会得到你开始的名字,这似乎有点无意义。