-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaccess.py
47 lines (32 loc) · 1.1 KB
/
access.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
import pyodbc
import pandas as pd
import warnings
class DBManager:
def __init__(self, db_path: str):
# Ignore pandas ODBC warning
warnings.filterwarnings('ignore')
self._conn_str = r'Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=' + db_path + ';'
try:
self.conn = pyodbc.connect(self._conn_str)
except Exception as e:
self._conn_str = r'Driver={Driver do Microsoft Access (*.mdb)};DBQ=' + db_path + ';'
self.conn = pyodbc.connect(self._conn_str)
self.conn.close()
def _open_connection(self):
self.conn = pyodbc.connect(self._conn_str)
def execute_dql(self, sql: str) -> pd.DataFrame:
self._open_connection()
df = pd.read_sql(sql, self.conn)
# Close connection
self.conn.close()
return df
def execute_dml(self, sql: str):
self._open_connection()
cursor = self.conn.cursor()
cursor.execute(sql)
self.conn.commit()
# Close connection
cursor.close()
self.conn.close()
if __name__ == '__main__':
pass