-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMySQL.py
More file actions
50 lines (34 loc) · 1.23 KB
/
Copy pathMySQL.py
File metadata and controls
50 lines (34 loc) · 1.23 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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import mysql.connector
class MySQL(object):
__Database = None
def __init__(self):
database = "solarstats"
password = "pass"
host = "127.0.0.1"
user = "root"
self.__Database = self.GetDB(database, user, password, host)
def GetDB(self, database, user="root", passwd=None, host="127.0.0.1"):
return mysql.connector.connect(user=user, password=passwd, host=host, database=database)
def Execute(self, sql):
return self.ExecuteDB(self.__Database, sql)
def ExecuteDB(self, db, sql):
cur = db.cursor()
cur.execute(sql)
return cur
def Fetchall(self, sql, index=None):
return self.FetchallDB(self.__Database, sql, index)
def FetchallCacheBreaker(self, sql, index=None):
return self.FetchallDB(self.__Database, sql, index)
def FetchallDB(self, db, sql, index=None):
result = []
for row in self.ExecuteDB(db, sql).fetchall():
if index is not None:
row = row[index]
result.append(row)
return result
def Disconnect(self):
self.DisconnectDB(self.__Database)
def DisconnectDB(self, db):
db.close()