This question has been flagged

Hi all,

there is a problem when try to select from db using condition (like '%word') such as the following example

            query = """
                SELECT sum(sale_price) as total, sum(sale_marg) as margin
                FROM boq_items as bi
                WHERE boq_project_id = %s and item_name NOT LIKE 'EXD-PS%' GROUP BY boq_project_id
            """%(pid)

the using of '%' is for pathing variables to the select statments like boq_project_id = %s it takes value of 's' from the end of statement %(pid) , but the problem is when try to using condition LIKE or NOT LIKE it contains '%' like my example i need to select where item_name doesn't contain 'EXD-PS' so when i use the above statement here is the problem because the compiler understand '%' as variable and except to find variable at the end of statement but that isn't true because here '%' is using for like condition so i get that error when try to using LIKE in select statement :

File "/home/habib/openerp7/addons/solution/solution.py", line 221, in _compute_margin_percentage """%(pid) TypeError: not enough arguments for format string

could anyone can help me on that ?

Thanks in advance

Avatar
Discard
Best Answer

You can use two percent signs %% to escape the % sicgn. Alternativly, you can try using the format method as follows:

query = "
                SELECT sum(sale_price) as total, sum(sale_marg) as margin
                FROM boq_items as bi
                WHERE boq_project_id = {0} and item_name NOT LIKE 'EXD-PS%' GROUP BY boq_project_id
            ".format(pid)

Avatar
Discard