pypyodbc

Output pyodbc cursor results as python dictionary

ⅰ亾dé卋堺 提交于 2019-11-26 15:17:17
问题 How do I serialize pyodbc cursor output (from .fetchone , .fetchmany or .fetchall ) as a Python dictionary? I'm using bottlepy and need to return dict so it can return it as JSON. 回答1: If you don't know columns ahead of time, use cursor.description to build a list of column names and zip with each row to produce a list of dictionaries. Example assumes connection and query are built: >>> cursor = connection.cursor().execute(sql) >>> columns = [column[0] for column in cursor.description] >>>