Skip to content

Commit

Permalink
Create class for msdb for optimization
Browse files Browse the repository at this point in the history
  • Loading branch information
jvdi committed Jul 17, 2022
1 parent 557a66b commit b6debf0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 13 deletions.
28 changes: 20 additions & 8 deletions db/db_con.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
11 changes: 6 additions & 5 deletions run.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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 = {
Expand Down Expand Up @@ -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()

0 comments on commit b6debf0

Please sign in to comment.