Odoo Help

Welcome!

This community is for beginners and experts willing to share their Odoo knowledge. It's not a forum to discuss ideas, but a knowledge base of questions and their answers.

0

Error when select from database using like '%word' ?

By
Ahmed elbarbary
on 2/19/15, 5:50 AM 704 views

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

1
Jordan Vrtanoski
On 2/19/15, 7:08 AM

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)

Your Answer

Please try to give a substantial answer. If you wanted to comment on the question or answer, just use the commenting tool. Please remember that you can always revise your answers - no need to answer the same question twice. Also, please don't forget to vote - it really helps to select the best questions and answers!

About This Community

This community is for professionals and enthusiasts of our products and services. Read Guidelines

Question tools

1 follower(s)

Stats

Asked: 2/19/15, 5:50 AM
Seen: 704 times
Last updated: 3/16/15, 8:10 AM