Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
Code snippit to export Atrex table metadata
import datetime
import logging
import sys
import pyodbc
import pandas as pd
logging.basicConfig(stream=sys.stdout, level=logging.INFO, datefmt="%Y-%m-%dT%H:%M:%S",
format='%(asctime)s.%(msecs)03d - %(levelname)s - %(message)s')
DSN = "<update to the correct DSN>"
QUERY = "select * from information.Tables"
try:"Initializing Connection")
connection = pyodbc.connect(f"DSN={DSN}")"Getting cursor")
cursor = connection.cursor()"Executing {QUERY}")
columns = [x[0] for x in cursor.description]
results = list()"Fetching records")
records = cursor.fetchall()"Result set contains {len(records)} records.")"Processing results of cursor.fetchall()")
for row in records:
results.append(dict(zip(columns, row)))
cursor.close()"Outputting to json")
pd.DataFrame(results).to_json(f'data/raw/{}-table-meta.json', 'records')
except pyodbc.Error as err:
if connection:
connection.close()"Execution complete")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment