There is a form for searching records in the database. How to create a query in Peewee: SELECT column1,column2,column3 FROM table WHERE field1 == 'value1' AND field2 LIKE '%value2%' AND field3 LIKE '%value3%' ?

The number of conditions in the request is not known in advance.

    1 answer 1

    Conditions can be collected by "paravozik" - they are connected through AND.

     params = [ (MyTable.column2 > 2), (MyTable.column1 == 'value1'), ... ] select = MyTable.select(...) for param in params: select = select.where(param) 

    here in the code select is going to

    • I tried to do it in a similar way, but in your example the number of fields is known, but I don’t - I don’t know how many form fields will be filled. - MyNick
    • Params - any generator, any number of fields - eri
    • and how to add values ​​to the param list? My conditions go there: if len (self.lineName.text ())! = 0: (add condition) - MyNick
    • Append if list - eri
    • tried so, added two values ​​in params - File "C: \ Python364 \ lib \ site-packages \ peewee.py", line 2567, in execute_sql cursor.execute (sql, params or ()) peewee.OperationalError: row value misused - MyNick