Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to extract data from SQL query and assign it to Odoo class columns?

I have been trying to extract data from a .mdb database and get it into Odoo 8 class columns.

This is my .py file

  class attendance_biometric(osv.Model):
    _name="attendance.biometric"
    _rec_name='name'
    _columns={

        'fdate':fields.datetime('From Date'),
        'tdate':fields.datetime('To Date'),
        'code':fields.integer('Code'),
        'name':fields.many2one('res.users','Employee Name', readonly=True),
        'ref': fields.one2many('bio.data', 'bio_ref', 'Data'),
    }

    _defaults = {
            'name': lambda obj, cr, uid, context: uid,

            }


def confirm_submit(self, cr, uid, ids, context=None):
        result=[]
        DBfile = '/home/administrator/test.mdb'
        conn = pyodbc.connect('DRIVER=MDBtools;DBQ='+DBfile)
        cr = conn.cursor()
        sql = '''
            select InTime, OutTime, OutDeviceId, Duration from 
AttendanceLogs '''
        cr.execute(sql)
        rows = cr.fetchall()
        for row in enumerate(rows):
            result.append(row)
        raise osv.except_osv(_('Info'),_('Data : %s\n' % (result)))

Now after some re-work when I click submit button, the data shows up like in the following images

Results in the logger info

Could someone provide valuable input on this? like how to get those values into Odoo class columns(I meant assigning to the fields that of the class) and also how to get columns from two tables.

like image 235
Shravy Avatar asked Jun 17 '16 10:06

Shravy


2 Answers

You need to understand the fetch types in odoo.

 - cr.dictfetchall()
       It will returns the list of dictionary.
       Example:
           [{'column1':'value_column1',}, {'column2':'value_column2',}] 

 - cr.dictfetchone() 
       It will return dictionary (Single record)
       Example:
           {'column1':'value_column1',}


 - cr.fetchall()
        It will returns the list of tuple.
        Example: 
            [('value_column1'), ('value_column2'), ].



 - cr.fetchone()
        It will returns the list of tuple.
        Example: 
            ('value_column1')

So update your code something like that,

res = cr.dictfetchall()
result['sname'] = res and res[0]['sname']

Whatever the values you want to set, all those must be returned by query.

However this is example you may need to update it according to your situation.

like image 101
Emipro Technologies Pvt. Ltd. Avatar answered Sep 22 '22 19:09

Emipro Technologies Pvt. Ltd.


Try to install /upgrade pyodbc version .. refer this link

like image 34
KbiR Avatar answered Sep 20 '22 19:09

KbiR