Answers for "sqlite3 info to dictionary list in python"

0

sqlite3 info to dictionary list in python

def sql_data_to_list_of_dicts(path_to_db, select_query):
      """Returns data from an SQL query as a list of dicts."""
      try:
          con = sqlite3.connect(path_to_db)
          con.row_factory = sqlite3.Row
          things = con.execute(select_query).fetchall()
          unpacked = [{k: item[k] for k in item.keys()} for item in things]
          return unpacked
      except Exception as e:
          print(f"Failed to execute. Query: {select_query}n with error:n{e}")
          return []
      finally:
          con.close()
Posted by: Guest on January-31-2022

Code answers related to "sqlite3 info to dictionary list in python"

Browse Popular Code Answers by Language