1

I retrieve the data from database and retrun it from this method.

  def _db_execute(s, query, data=None, return_data=False):

        con = mdb.connect(s.sql_detail['host'], s.sql_detail['user'], s.sql_detail['pass'], s.sql_detail['db'], cursorclass=mdb.cursors.DictCursor)
        with con:
            cur = con.cursor()
            if type(data) in [ list, tuple ] and len(data) != 0:
                if data[0] in [ list, tuple ]:
                    cur.executemany(query, (item for item in data))
                else:
                    cur.execute(query, data)
            elif data != None:
                cur.execute(query, [data])
            else:
                cur.execute(query)
            if return_data:
                data = cur.fetchall()
                if len(data) == 0:
                    data = None
                return data

Following method retrieve data.

 def retrieve_portal_credetials(self):

        if(self.valid_user()):

            query2 ='''SELECT `group`,`username`,`password` FROM db.users u, db.groups g, db.user_groups i where u.userid=i.user_id and g.group_id = i.groupd_id and u.username=%s'''

            portal_data= self._db_execute(query=query2, data = self.username, return_data = True)

        return portal_data

I try to assign the data to variables here

rows = auth_usr.retrieve_portal_credetials()



                                #list_of_portaldata = list(portal_data)
                                #s.data.update({'groups_list': [val]})
                                #val=   list_of_portaldata

                                for row in rows:


                                    portal_group = row[0]
                                    portal_username = row[1]
                                    portal_password = row[2]                                

When I debug through the code, I found it breaks here portal_group = row[0] and I got the error KeyError: 0

What I understood was, row doesn't has 0 key. That's why I get this error. However, in the debugger it shows 3 variables under row. Can anybody give any hint to fix this?

ivan_pozdeev
  • 33,874
  • 19
  • 107
  • 152
wordpressm
  • 3,189
  • 3
  • 20
  • 29
  • `hasattr(obj,'__iter__')` or `isinstance(e, collections.Iterable)` is the canonical way to check for iterable (http://stackoverflow.com/questions/1952464/in-python-how-do-i-determine-if-an-object-is-iterable) – ivan_pozdeev Mar 10 '15 at 16:00
  • Is `row` a dictionary-like object or a list-like object? The fact you get `KeyError` rather than `IndexError` suggests it's dict-like... – Tom Dalton Mar 10 '15 at 16:23

1 Answers1

1

After inspecting the datastructure shows in the debugger.

I use the following syntax to access the data.

portal_group = row['group']
portal_username = row['username']
portal_password = row['password'] 
wordpressm
  • 3,189
  • 3
  • 20
  • 29