odoo sql 语句 查询数据库

 1 import psycopg2
 2 
 3 class XXXXX(models.Model):
 4         ........
 5 
 6     @api.multi
 7     def OOOO(self):
 8         db = psycopg2.connect("dbname=test4 user=postgres")#查找名叫 test4 的数据库  postgres  是数据库的超级用户名称
 9         vals = db.cursor()
10         vals.execute("SELECT sales,prices,sale_date FROM run_chart")#执行sql语句查询数据
11         tables = vals.fetchall()#返回查询结果

此代码用的数据库为:postgresql

odoo 还有另外一种更加方便的SQL查询方式

例子:

sql = "select *from 表名"
self.env.cr.execute(sql)  #执行SQL语句
dicts = self.env.cr.dictfetchall()  #获取SQL的查询结果
原文地址:https://www.cnblogs.com/brucexl/p/7171163.html