Output pyodbc cursor results as python dictionary -


how serialize pyodbc cursor output (from .fetchone, .fetchmany or .fetchall) python dictionary?

i'm using bottlepy , need return dict can return json.

if don't know columns ahead of time, use cursor.description build list of column names , zip each row produce list of dictionaries. example assumes connection , query built:

>>> cursor = connection.cursor().execute(sql) >>> columns = [column[0] column in cursor.description] >>> print columns ['name', 'create_date'] >>> results = [] >>> row in cursor.fetchall(): ...     results.append(dict(zip(columns, row))) ... >>> print results [{'create_date': datetime.datetime(2003, 4, 8, 9, 13, 36, 390000), 'name': u'master'},     {'create_date': datetime.datetime(2013, 1, 30, 12, 31, 40, 340000), 'name': u'tempdb'},  {'create_date': datetime.datetime(2003, 4, 8, 9, 13, 36, 390000), 'name': u'model'},       {'create_date': datetime.datetime(2010, 4, 2, 17, 35, 8, 970000), 'name': u'msdb'}] 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -