0

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 codereturn [((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")

navarian
  • 41
  • 1
  • 10
  • As an aside , because you already have an answer, why are you fetching ALL rather than limiting the query to 1 ? You are after all only using the first record that comes back. p.s. `len(row[0])` – Rolf of Saxony May 22 '16 at 15:59

1 Answers1

0

You could use map() function. Try

return map( lambda x: (x[0].encode("utf-8"),)+x[1:], row )
kvorobiev
  • 5,012
  • 4
  • 29
  • 35