-
Notifications
You must be signed in to change notification settings - Fork 0
/
mysql_tools.py
97 lines (83 loc) · 3.31 KB
/
mysql_tools.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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
################################################################################
# mysql_tools.py
# This module contains classes which facilitate MySQL queries.
# -------------------
# Required Packages
# - mysql
# - mysql.connector
# - pandas
################################################################################
# package management
import mysql
import mysql.connector
import pandas as pd
################################################################################
# sql_connection
# The sql_connection class facilitates access to a MySQL database
# using mysql.connector.
# METHODS
#---------------------------------------------------------------
# sql_connection._open() - Opens the connection to the database
#---------------------------------------------------------------
# sql_connection._close() - Closes the connection to the database
#---------------------------------------------------------------
# sql_connection.use_db() - Sets the DATABASE to be used in
# the connection.
#---------------------------------------------------------------
class sql_connection:
# initialize the instance
def __init__(self,db=None):
self.host = 'localhost'
self.user = 'python_connection'
self.password = 'demo_pass'
self.database = db
self.connection = None
self.cursor = None
# open the connection
def _open(self):
self.connection = mysql.connector.connect(host=self.host,
user=self.user,
password=self.password,
database=self.database)
# close the connection
def _close(self):
self.connection.close()
# set the DATABASE
def use_db(self,db):
self.database = db
################################################################################
# easy_sql
# The easy_sql class connects to a mysql
# database and executes queries. At
# present, the query can be returned as
# a pandas DataFrame or a NumPy array.
# METHODS
#---------------------------------------------------------------
# easy_sql.query_to_pandas() - Executes a query given in string
# format and returns the results
# in a pandas DataFrame.
#---------------------------------------------------------------
# easy_sql.query_to_numpy() - Executes a query given in string
# format and returns the results
# in a NumPy array.
#---------------------------------------------------------------
class easy_sql(sql_connection):
# initialize the instance
def __init__(self,db=None):
sql_connection.__init__(self,db=None)
# run the query and put the results
# in a pandas DataFrame
def query_to_pandas(self,query):
self._open()
df = pd.read_sql(query, con=self.connection)
self._close()
return df
# run the query and put the results
# in a NumPy array
def query_to_numpy(self,query):
self._open()
df = pd.read_sql(query, con=self.connection)
np_array = df.as_matrix()
self._close()
return np_array
################################################################################