i have a function :
def tong_thoigian (self,cr,uid,ids,context={}):
obj=self.browse(cr,uid,ids,context=context)[0]
cr.execute('''select name,giolam from x_giolam where name=%s'''%(obj.ma_luong))
kq=cr.fetchall()
tong=0.00000
for i in kq:
tong+=kq[1]
self.write(cr,uid,ids,{'tonggiolam':tong},context=context)
and this is table x_giolam:
class x_giolam(osv.osv):
_name = 'x_giolam'
_description = 'Gio Lam'
_columns = {
'name': fields.integer('Lọai',size=64,required="true"),
'giolam' : fields.float('Gio lam',size=64,required="True"),
'time_in': fields.char('Gio vào',size=20),
'time_out' :fields.char('Gio về',size=20),
'congviec' :fields.char('Cong viec',size=50),
}
x_giolam()
and the ‘self’ is table x_salary, i think isn’t importance to say about it because i want write a function for sum salary of a staff when name=Ma_luong of table x_salary
and the error is
IndexError: list index out of range
the type of Giolam is float…
and i write with in openerp
and i think error in line ‘tong+=kq[1]’
How can i fix it ?
thanks!!
Using my magic crystal ball, I’m guessing that
cr.executeis a call to the standard database API. Sokr.fetchall()will return a tuple of rows. However, it seems that your SQL is returning only a single row.You probably mean
tong += kq[0][1], ie the second column (giolam) of the first row of the result. Alternatively, usekr.fetchone()to just get a single row, then you can keep it askq[1]. Either way, you should check that your db call actually returns results.