-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMSSqlDB.py
59 lines (49 loc) · 1.95 KB
/
MSSqlDB.py
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
import pyodbc
import configparser
class MSSqlDBManager:
def __init__(self, AccountSet):
self.config_file = 'config.ini'
# AccountSet代表账套,上海账套使用"希肤上海",广州账套使用"希肤广州"
self.AccountSet = AccountSet
self.config = self.load_config()
self.connection = self._connect()
def load_config(self):
config = configparser.ConfigParser()
config.read(self.config_file, encoding='utf-8')
if self.AccountSet == '希肤广州':
return config['DatabaseGZ']
elif self.AccountSet == '希肤上海':
return config['DatabaseSH']
def __enter__(self):
self.connection = self._connect()
return self
def __exit__(self, exc_type, exc_val, exc_tb):
self.close_connection()
def _connect(self):
connection_string = f"""
DRIVER={{SQL Server}};
SERVER={self.config['server']};
DATABASE={self.config['database']};
UID={self.config['username']};
PWD={self.config['password']};
Encrypt=no;
"""
return pyodbc.connect(connection_string)
def execute(self, command, params=None, fetch=False):
with self.connection.cursor() as cursor:
if params:
cursor.executemany(command, params)
else:
cursor.execute(command)
if fetch:
return cursor.fetchall()
self.connection.commit()
def execute_delete(self,tablename, finterid, fentryid):
sql = f"DELETE FROM {tablename} WHERE interid = ? AND entryid = ?"
with self.connection.cursor() as cursor:
cursor.execute(sql, (finterid, fentryid))
self.connection.commit()
return cursor.rowcount
def close_connection(self):
if self.connection:
self.connection.close()