[PyGreSQL] DB 2.0 list of tuples
Christoph Zwerschke
cito at online.de
Fri Nov 17 17:37:55 EST 2006
> I'm trying out the pgdb interface for
> the first time. One surprise was that the
> results in fetchall() are returned as a list
> of lists. The DB 2.0 specification says the
> results are a list of tuples. Is there a way
> to get a list of tuples returned? I have some
> applications that require this format, and
> copying the result is not going to be
> efficient.
No, the DB API 2 specification does not say it is a list of tuples. It
says it is a "sequence of sequences (e.g. a list of tuples)". So it
could as well be a list of lists (as in pgdb) or a tuple of tuples or a
list of some special row objects. All these different implementations
exist. So if your application wants to be DB API 2 conform, it should
not assume anything here. If you need something specific (normally it
should not matter), use tuple(...) or list(...) to convert it
explicitly. Don't worry, this will not copy the whole result:
>>> a = ['test']
>>> b = tuple(a)
>>> b[0] is a[0]
True
The reason why pgdb (contrary to pg) returns a list of lists is that the
rows are typecasted in pgdb and this process converts them to lists. It
would require an additional step to convert them back to tuples. I see
not real reason why PyGreSQL should do that other that it would be more
consistent with the implementation in the classic pg interface and also
in psycopg2 (but not in some other DB API 2 interfaces).
-- Chris
More information about the PyGreSQL
mailing list