-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdata_connector.py
More file actions
62 lines (54 loc) · 2.03 KB
/
data_connector.py
File metadata and controls
62 lines (54 loc) · 2.03 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
import json
import pymssql
class MSSQLConnection:
def __init__(self, settings_filename='settings.json'):
self.__connected = False
with open(settings_filename, 'r', encoding='utf-8') as f:
settings = json.loads(f.read())
self.__server = settings['server']
self.__database = settings['database']
self.__login = settings['login']
self.__password = settings['password']
self.__driver = "{SQL Server Native Client 11.0}"
try:
self.__connection = pymssql.connect(self.__server, self.__login, self.__password, self.__database)
self.__connected = True
except Exception as E:
print(f"Exception while creating connection class: {E}")
self.__connected = False
self.__connection = None
@property
def connection(self):
return self.__connection
@property
def connected(self):
return self.__connected
def execute(self, query, params=()):
if self.connection is not None:
try:
if len(params) > 0:
self.connection.cursor().execute(query, params)
self.connection.commit()
else:
self.connection.cursor().execute(query)
self.connection.commit()
return True
except Exception as E:
print(f"Exception while execute: {E}")
return False
else:
return False
def select(self, query, params=()):
if self.connection is not None:
try:
cursor = self.connection.cursor()
if len(params) > 0:
cursor.execute(query, params)
else:
cursor.execute(query)
for row in cursor:
yield [row[el] for el in range(0, len(row))]
cursor.close()
except Exception as E:
print(f'Exception while select: {E}')
return ()