-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsqlsrvrHandler.py
More file actions
42 lines (32 loc) · 1.46 KB
/
sqlsrvrHandler.py
File metadata and controls
42 lines (32 loc) · 1.46 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
import pyodbc
import pandas as pd
# -------------------------------------------------------------------
class SQLServer():
def __init__(self, SERVER, DATABASE, DRIVER='ODBC Driver 17 for SQL Server'):
self.SERVER = SERVER
self.DATABASE = DATABASE
self.DRIVER = DRIVER
self.connection = self.connect()
def getDrivers(self):
return pyodbc.drivers()
def setDriver(self):
for index, driver in enumerate(self.getDrivers()):
print('{}. {}\n'.format(index, driver))
driverSelect = input('Enter Driver Number : ')
self.DRIVER = self.getDrivers()[int(driverSelect)]
print('Driver Selected : {}'.format(self.DRIVER))
print('[LOG] Reconnecting')
self.connect()
def connect(self):
print('[LOG] Connecting to SQL Server')
print(' Driver: {}'.format(self.DRIVER))
print(' Server: {}'.format(self.SERVER))
print(' Database: {}'.format(self.DATABASE))
return pyodbc.connect("Driver={" + self.DRIVER + "};"
"Server=" + self.SERVER + ";"
"Database=" + self.DATABASE + ";"
"Trusted_Connection=yes;")
def queryToDF(self, query, chunksize=None):
print('[LOG] Reading SQL Query')
return pd.read_sql_query(query, self.connection, chunksize=chunksize)
# -------------------------------------------------------------------