Re: KeyError: self._index[x] - Mailing list pgsql-general

From David G. Johnston
Subject Re: KeyError: self._index[x]
Date
Msg-id CAKFQuwbQ4NFdOsnynqK6OAmdbFUsOjT+LtuNdfA6SFqBCQc6YA@mail.gmail.com
Whole thread Raw
In response to Re: KeyError: self._index[x]  (tango ward <tangoward15@gmail.com>)
Responses Re: KeyError: self._index[x]  (Adrian Klaver <adrian.klaver@aklaver.com>)
List pgsql-general
On Monday, May 7, 2018, tango ward <tangoward15@gmail.com> wrote:

cur_t.execute("""
                SELECT TRANSLATE(snumber, ' ', '')
                FROM sprofile """)

# This will result in KeyError
for row in cur_t:
print row['snumber']

# This works fine
for row in cur_t:
print row[0]

So apparently when you execute your query the result has at least one column but that column isn't named "snumber".  I'm sure there is a way in Python to debug "row" and find out what names it does have.  Or maybe execute the query in something like psql and observe e column name there.

That said, by default the name of columns whose values are derived by a single function call should be the name of the function.  So "translate", not "snumber" - the latter being consumed by the function.  You can as use "as <alias>" to give it a different fixed name and refer to that.

David J.

pgsql-general by date:

Previous
From: tango ward
Date:
Subject: Re: KeyError: self._index[x]
Next
From: Adrian Klaver
Date:
Subject: Re: KeyError: self._index[x]