diff --git a/db/db_con.py b/db/db_con.py index 34fcf0a..839d6bd 100644 --- a/db/db_con.py +++ b/db/db_con.py @@ -26,14 +26,26 @@ def close(self): self.sqlite_cur.close() -# Connection for mssql -msSqlCon = pymssql.connect( - host=os.getenv('MSSQL_HOST'), - user=os.getenv('MSSQL_USER'), - password=os.getenv('MSSQL_PASSWORD'), - database=os.getenv('MSSQL_db') -) -ms_cur = msSqlCon.cursor(as_dict=True) # Create a dic and move data to it +class MsSql: + def __init__(self): + # Connection for mssql + self.msSqlCon = pymssql.connect( + host=os.getenv('MSSQL_HOST'), + user=os.getenv('MSSQL_USER'), + password=os.getenv('MSSQL_PASSWORD'), + database=os.getenv('MSSQL_db') + ) + # Create a dic and move data to it + self.ms_cur = self.msSqlCon.cursor(as_dict=True) + + def execute(self, sql): + self.ms_cur.execute(sql) + + def fetch(self): + return self.ms_cur + + def close(self): + self.msSqlCon.close() # Create table in sqlite for save pay-log diff --git a/run.py b/run.py index 0884721..3b16d24 100644 --- a/run.py +++ b/run.py @@ -22,6 +22,7 @@ def relative_to_assets(path: str) -> Path: # Get Row from mssql # Exist Column: RowID, Fix_Acc1_ID, Fix_Acc2Type_ID, BedPrice, RowDesc, ... + ms_cur = MsSql() ms_cur.execute(''' SELECT * FROM DocD @@ -30,11 +31,13 @@ def relative_to_assets(path: str) -> Path: ''') # Get price and document id from mssql - for row in ms_cur: + for row in ms_cur.fetch(): global price_to_send, doch_id price_to_send = (int(row['BedPrice'])*10) doch_id = row['DocH_ID'] break + + ms_cur.close() # Set data for sending to pay-terminal data = { @@ -172,7 +175,5 @@ def abort_pay(): '''.format(doch_id, price_to_send, json['PcPosStatusCode'])) sqlite.commit() - # Close all database - sqlite.close() - - # msSqlCon.close() + # Close sqlite connection + sqlite.close() \ No newline at end of file