SQL statment in odoo 9 function

506 views Asked by At

How in function create sql statment in odoo 9?

I want call my_function and print all record from database.

For example: select time_start from time where time_start >= 'TODAY''

def my_function(self):
        result = []
        for data in self.browse():
            cr.execute('''select
                    time_start
                from
                    time
                where
                    time_start >= 'TODAY''')
        print all result line by line
1

There are 1 answers

3
Andre Buschermöhle On BEST ANSWER

Try

def my_function(self):
   result = []
   for data in self.browse():
      cr.execute('''select
                    time_start
                from
                    time
                where
                    time_start >= 'TODAY''')

      for line in cr.dictfetchall():
         print line["time_start"]
         print line

cr.dictfetchall() returns a list of dicts. Every element from this list represents a line in your query-result.

In my solution I iterate over this list and can directly access the field by fieldname in database.

Please note, that use query in a loop. Probably there is a better way.

EDIT: Try search instead of browse. browse gives you the matching records to given ids.

def my_function(self):
   result = []
   for data in self.search([]):
      cr.execute('''select
                    time_start
                from
                    time
                where
                    time_start >= 'TODAY''')

      for line in cr.dictfetchall():
         print line["time_start"]
         print line