145 """ |
145 """ |
146 header = reader.next() |
146 header = reader.next() |
147 for row in reader: |
147 for row in reader: |
148 yield dict(zip(header, row)) |
148 yield dict(zip(header, row)) |
149 |
149 |
150 def lazydbtable(cu, table, headers): |
150 def lazydbtable(cu, table, headers, orderby=None): |
151 """return an iterator on rows of a sql table. On each row, fetch columns |
151 """return an iterator on rows of a sql table. On each row, fetch columns |
152 defined in headers and return values as a dictionary. |
152 defined in headers and return values as a dictionary. |
153 |
153 |
154 >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps')) |
154 >>> data = lazydbtable(cu, 'experimentation', ('id', 'nickname', 'gps')) |
155 """ |
155 """ |
156 cu.execute('SELECT %s FROM %s' % (','.join(headers), table,)) |
156 sql = 'SELECT %s FROM %s' % (','.join(headers), table,) |
|
157 if orderby: |
|
158 sql += ' ORDER BY %s' % ','.join(orderby) |
|
159 cu.execute(sql) |
157 while True: |
160 while True: |
158 row = cu.fetchone() |
161 row = cu.fetchone() |
159 if row is None: |
162 if row is None: |
160 break |
163 break |
161 yield dict(zip(headers, row)) |
164 yield dict(zip(headers, row)) |