Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.python > #45679

More general way of generating PyODBC queries as a dict?

X-Received by 10.180.21.226 with SMTP id y2mr1077552wie.5.1369157227765; Tue, 21 May 2013 10:27:07 -0700 (PDT)
X-Received by 10.50.39.84 with SMTP id n20mr1853416igk.13.1369157227245; Tue, 21 May 2013 10:27:07 -0700 (PDT)
Path csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!eu.feeder.erje.net!de-l.enfer-du-nord.net!feeder2.enfer-du-nord.net!feeder1.enfer-du-nord.net!usenet-fr.net!proxad.net!feeder1-2.proxad.net!209.85.212.215.MISMATCH!lg1no22841590wic.0!news-out.google.com!hv6ni5224wib.1!nntp.google.com!h2no20446772wiw.1!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail
Newsgroups comp.lang.python
Date Tue, 21 May 2013 10:27:07 -0700 (PDT)
Complaints-To groups-abuse@google.com
Injection-Info glegroupsg2000goo.googlegroups.com; posting-host=58.165.188.19; posting-account=X1MLHQoAAADj-_XNRmphqLB0BjYmNmvJ
NNTP-Posting-Host 58.165.188.19
User-Agent G2/1.0
MIME-Version 1.0
Message-ID <0f78102c-2e30-4858-ae62-b8d5cecfc345@googlegroups.com> (permalink)
Subject More general way of generating PyODBC queries as a dict?
From stackoverflowuser95@gmail.com
Injection-Date Tue, 21 May 2013 17:27:07 +0000
Content-Type text/plain; charset=ISO-8859-1
Xref csiph.com comp.lang.python:45679

Show key headers only | View raw


Here are my averagely general class methods for creating a dictionary from the result of database queries:

def make_schema_dict(self):
    schema = [i[2] for i in self.cursor.tables()
              if i[2].startswith('tbl_') or i[2].startswith('vw_')]

    self.schema = {table: {'scheme': [row.column_name for row
                                      in self.cursor.columns(table)]}
                   for table in schema}

def last_table_query_as_dict(self, table):
    return {'data': [{col: row.__getattribute__(col) for col in self.schema[table]['scheme']
                      if col != 'RowNum'} for row in self.cursor.fetchall()]}
Unfortunately as you can see, there are many complications.

For example, when multiple tables are queried; some hackish lambdas are required to generate the resulting dictionary.

Can you think of some more general methods?

(and yes I know this is against the PEP; and that this is also on SO)

Back to comp.lang.python | Previous | NextNext in thread | Find similar | Unroll thread


Thread

More general way of generating PyODBC queries as a dict? stackoverflowuser95@gmail.com - 2013-05-21 10:27 -0700
  Re: More general way of generating PyODBC queries as a dict? Dennis Lee Bieber <wlfraed@ix.netcom.com> - 2013-05-21 19:33 -0400
  Re: More general way of generating PyODBC queries as a dict? stackoverflowuser95@gmail.com - 2013-05-22 02:31 -0700

csiph-web