neilH
neilH

Reputation: 3438

psycopg2 - TypeError: 'int' object is not subscriptable

I'm performing the postgres query with psycopg2, stored in function columnsearch(). My aim is to be able to view the data in each field by using the column name in the return statement. I'm receiving the error:

TypeError: 'int' object is not subscriptable

Does anyone know how I can rectify this?

def columnsearch():
     con = psycopg2.connect(connectstring)
     cur = con.cursor(cursor_factory=e.DictCursor)
     cur.execute("SELECT * FROM radio_archive_ind.radio_archive_ind WHERE tape_number = 'TP00001'")
     rows = cur.fetchone()
     for r in rows:
             return r["tape_number"]

print columnsearch()

Upvotes: 2

Views: 2515

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169384

Note that .fetchone() returns a single sequence so you can just do:

def columnsearch():
     con = psycopg2.connect(connectstring)
     cur = con.cursor(cursor_factory=e.DictCursor)
     cur.execute("""SELECT * 
                      FROM radio_archive_ind.radio_archive_ind 
                     WHERE tape_number = 'TP00001'""")
     row = cur.fetchone()
     return row["tape_number"]

Upvotes: 1

Related Questions