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?