updated to output to json file

This commit is contained in:
Jonathan Branan 2024-06-24 17:46:00 -05:00
parent 70d1999ca4
commit d41f685f5c
3 changed files with 15 additions and 5 deletions

11
inex.py
View File

@ -5,6 +5,7 @@ from datetime import datetime
from tomllib import load
import inexLogging
import inexConnect
from json import dump,dumps
class Inex:
def __init__(self):
@ -28,13 +29,19 @@ class Inex:
self.dbUser = self.config["database"]["user"]
self.dbPassword = self.config["database"]["password"]
self.dbQuery = self.config["database"]["query"]
self.outputFile = self.config["output"]["filename"]
# create the connection to the database
self.cursor = self.ic.connectDatabase(self.db, self.dbDriver, self.dbServer, self.dbDatabase, self.dbUser, self.dbPassword)
self.query = self.ic.databaseQuery(self.cursor, self.dbQuery)
self.data = self.ic.databaseQuery(self.cursor, self.dbQuery)
# self.jsonData = dumps(self.data)
# print(self.jsonData)
with open(self.outputFile, "w") as f:
dump(self.data, f)
print(self.query)
# Run
if __name__== "__main__":

View File

@ -2,9 +2,12 @@ def connectDatabase(lib, driver, server, database, user, password):
connectionString = f'DRIVER={{ODBC Driver 18 for SQL Server}};SERVER={server};DATABASE={database};UID={user};PWD={password};TrustServerCertificate=yes'
print(connectionString)
connection = lib.connect(connectionString)
# connection = lib.connect(f'DRIVER={{driver}};SERVER={server};DATABASE={database}')
cursor = connection.cursor()
return cursor
def databaseQuery(cursor, query):
cursor.execute(query)
def databaseQuery(cursor, query, args=(), one=False):
cur = cursor.execute(query, args)
r = [dict((cur.description[i][0], value) \
for i, value in enumerate(row)) for row in cur.fetchall()]
cur.connection.close()
return (r[0] if r else None) if one else r