kostasdi kostasdi - 6 months ago 34
MySQL Question

encode the elements of a tuple

i have this code which returns the values of a database using a tuple

# Create a new connection
con=connection()

#create a cursor to the connection
cur=con.cursor()

cur.execute("SELECT tragoudi.titlos, tragoudi.etos_par, cd_production.etaireia FROM tragoudi JOIN singer_prod ON tragoudi.titlos=singer_prod.title JOIN cd_production ON singer_prod.cd=cd_production.code_cd GROUP BY tragoudi.titlos HAVING tragoudi.titlos LIKE %s AND tragoudi.etos_par LIKE %s AND cd_production.etaireia LIKE %s",(titlos,etos_par,etaireia,))
con.commit()

row = cur.fetchall()
return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]


All the characters in
row[:][0]
are greek letters and i must encode them so i will not get as an answer
(u'\u0391\u0393\u03a9\u039d\u0399\u0391', 1978, u'SONY')


but
(u'ΑΓΩΝΙΑ', 1978, u'SONY')


The problem here is that i dont know the lenght of the tuple.
I know this line of code
return [((row[0][0]).encode("utf-8"),(row[0][1]),(row[0][2])),]
is wrong because it gives me this error
IndexError('tuple index out of range',)


and i am wondering if there is a way to encode all the elements whose are at the
row[something][0]
by using something like
row[:][0].encode("utf-8")

Answer

You could use map() function. Try

return map( lambda x: (x[0].encode("utf-8"),)+x[1:], row )