A
A
AirronBark2021-03-14 12:11:49
Python
AirronBark, 2021-03-14 12:11:49

How to do a SQLite3 table search on multiple columns?

Good to everyone.

I am making a program using python tkinter for myself and of course I am new to this business.
In the database, one table (so far) has data in 5 columns (the first id). It is necessary that it give out search results (when the button is clicked) on them, that is, the user enters, for example: the name of the product, dimensions (length/width/thickness).
Here is the db access function itself:

def search_records(self, product, lenght, width, thickness):
        product = ('%' + product + '%',) 
        lenght = ('%' + lenght + '%',)
        width = ('%' + width + '%',)
        thickness = ('%' + thickness + '%',)
        self.db.c.execute('''SELECT * FROM massa WHERE product LIKE ? AND lenght  LIKE ? AND  width LIKE ? AND thickness LIKE ?''', product, lenght, width, thickness)
        [self.tree.delete(i) for i in self.tree.get_children()]
        [self.tree.insert('', 'end', values=row) for row in self.db.c.fetchall()]


The button is described here:
def init_search(self):
        self.title('Поиск')
        self.geometry ('500x300+400+300')
        self.resizable(False, False)
        label_search = tk.Label(self, text = 'Поиск по таблице')
        label_search.place(x=250,y=20)

        label_product_s = tk.Label(self, text='Наименование продукции:')
        label_product_s.place(x=50, y=80)
        self.entry_product_s = ttk.Entry(self)
        self.entry_product_s.place(x=300, y=80)

        label_lenght_s = tk.Label(self, text='Длина, мм:')
        label_lenght_s.place(x=50, y=110)
        self.entry_lenght_s = ttk.Entry(self) 
        self.entry_lenght_s.place(x=300, y=110)

        label_width_s = tk.Label(self, text='Ширина, мм:')
        label_width_s.place(x=50, y=140)
        self.entry_width_s= ttk.Entry(self) 
        self.entry_width_s.place(x=300, y=140)

        label_thickness_s = tk.Label(self, text='Толщина или высота, мм:')
        label_thickness_s.place(x=50, y=170)
        self.entry_thickness_s = ttk.Entry(self) 
        self.entry_thickness_s.place(x=300, y=170)
        btn_cansel = ttk.Button(self, text='Закрыть', command=self.destroy) 
        btn_cansel.place(x=185, y=230)
        btn_search = ttk.Button (self, text='Поиск')
        btn_search.place(x=105, y=230)
        btn_search.bind('<Button-1>', lambda event:self.view.search_records(self.entry_product_s.get(),self.entry_lenght_s.get(), self.entry_width_s.get(), self.entry_thickness_s.get()))
        btn_search.bind('<Button-1>', lambda event:self.destroy(), add='+')


And this is the error that occurs when starting the search:
Exception in Tkinter callback
Traceback (most recent call last):
  File "C:\Program Files (x86)\Python38-32\lib\tkinter\__init__.py", line 1883, in __call__
    return self.func(*args)
  File "D:/Phyton/App_big_project/Main_app.py", line 231, in <lambda>
    btn_search.bind('<Button-1>', lambda event:self.view.search_records(self.entry_product_s.get(),self.entry_lenght_s.get(), self.entry_width_s.get(), self.entry_thickness_s.get()))
  File "D:/Phyton/App_big_project/Main_app.py", line 80, in search_records
    self.db.c.execute('''SELECT * FROM massa WHERE product LIKE  ? AND lenght  LIKE  ? AND  width LIKE  ? AND thickness LIKE ?''', product, lenght, width, thickness) 
TypeError: function takes at most 2 arguments (5 given)

Therefore, the question is what I need to fix in the search function or in the button so that I need the result. Or is this not implemented at all in these libraries?)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
Vladimir Kuts, 2021-03-14
@AirronBark

(product, length, width, thickness) are passed as one argument (put in parentheses)

A
AirronBark, 2021-03-14
@AirronBark

If anyone has an analogue error: Fixed the last error by adding str in the code: product = str ('%' + product + '%',).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question