首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >odoo 9函数中的SQL语句

odoo 9函数中的SQL语句
EN

Stack Overflow用户
提问于 2016-12-19 14:58:51
回答 1查看 500关注 0票数 1

函数如何在odoo 9中创建sql语句?

我要打电话给my_function,从数据库打印所有记录。

例如:从“时间”中选择“time_start”,其中time_start >=“当前”

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-12-19 16:22:46

试一试

代码语言:javascript
复制
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()返回一个dicts列表。此列表中的每个元素都表示查询结果中的一行。

在我的解决方案中,我迭代这个列表,并可以通过数据库中的字段名直接访问该字段。

请注意,在循环中使用查询。也许还有更好的办法。

编辑:尝试search而不是browsebrowse为您提供与给定ids匹配的记录。

代码语言:javascript
复制
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
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/41225365

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档