-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMYSQL TOOLS.py
171 lines (137 loc) · 6.09 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
import pymysql.cursors
from datetime import datetime
import logging
from AwsStuff.sc_creds import AWS_MYSQL_ADD, AWS_MYSQL_USER, AWS_MYSQL_PW
class Mover:
def __init__(self):
if len(logging.getLogger().handlers) > 0:
# The Lambda environment pre-configures a handler logging to stderr. If a handler is already configured,
# `.basicConfig` does not execute. Thus we set the level directly.
logging.getLogger().setLevel(logging.INFO)
else:
logging.basicConfig(level=logging.INFO)
""" CHILD TABLE HYPHEN ` REQUIRED ONLY WHEN REFERRING TO GAMENUMBER TABLES"""
""" end of INIT """
@staticmethod
def log(cls, string):
logging.error(string)
print(string)
def connect(self):
try:
REGION = 'eu-west-2a'
port = 3306
self.connection = pymysql.connect(AWS_MYSQL_ADD,
user=AWS_MYSQL_USER,
passwd=AWS_MYSQL_PW,
db="scdb",
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
connect_timeout=5)
self.mycursor = self.connection.cursor()
except self.connection.Error as error:
print("Error connecting to MYSQL DB")
# self.log(self, str(self.scratchcard.gamenumber) + " Error connecting to MYSQL DB.")
def disconnect(self):
self.mycursor.close()
self.connection.close()
def __repr__(self):
print(str(self.connection.server_status()))
def __exit__(self, exc_type, exc_val, exc_tb):
self.disconnect()
def main(self):
self.list_of_logs = list()
""" GET List of Tables"""
self.mycursor.execute("SHOW TABLES")
for x in self.mycursor:
self.list_of_logs.append(x['Tables_in_scdb'])
print(self.list_of_logs)
self.newlist = list()
for item in self.list_of_logs:
try:
self.newlist.append(int(item))
except ValueError:
print("nope" + str(item))
print(self.newlist)
""" INSERT INTO sc_log"""
for game in self.newlist:
print(game)
self.mycursor.execute(f"INSERT INTO sc_log SELECT * FROM `{game}`;")
self.connection.commit()
def cleaner(self):
self.mycursor.execute(f"SELECT gamenumber FROM main")
x = self.mycursor.fetchall()
self.list_games = list()
for game in x:
self.list_games.append(game['gamenumber'])
rows_to_delete = list()
for game in self.list_games:
self.mycursor.execute(f"SELECT * FROM sc_log WHERE gnumber={game} ORDER BY remainingtop DESC")
x = self.mycursor.fetchall()
tempdates = []
for rowdata in x:
if rowdata['lastupdate'] not in tempdates: # if its a new date
tempdates.append(rowdata['lastupdate']) # add to list
else:
rows_to_delete.append(rowdata) # not a new date
print("To delete: " + str(rowdata))
for row in rows_to_delete:
print("Deleting: " + str(row))
# self.mycursor.execute("SHOW ENGINE INNODB STATUS")
self.mycursor.execute(f"DELETE FROM sc_log WHERE gnumber='{row['gnumber']}' AND "
f"remainingtop='{row['remainingtop']}' AND "
f"lastupdate='{row['lastupdate']}' AND "
f"datestarted='{row['datestarted']}';")
self.connection.commit()
def earliest(self): # set all to earliest datestarted
self.mycursor.execute(f"SELECT gamenumber FROM main")
x = self.mycursor.fetchall()
self.list_games = list()
for game in x:
self.list_games.append(game['gamenumber'])
rows_to_delete = list()
for game in self.list_games:
self.mycursor.execute(f"SELECT * FROM sc_log WHERE gnumber={game} ORDER BY 'datestarted' DESC")
gameset = self.mycursor.fetchall()
""" Get earliest datestarted from game"""
tempdate = datetime.now().date()
for rowdata in gameset:
if rowdata['datestarted'] < tempdate:
tempdate = rowdata['datestarted']
""" update all gameset with oldest datestarted"""
self.mycursor.execute(f"UPDATE sc_log SET datestarted='{tempdate}' WHERE gnumber={game};")
self.connection.commit()
def duplicate_rt(self):
self.mycursor.execute(f"SELECT gamenumber FROM main")
x = self.mycursor.fetchall()
self.list_games = list()
for game in x:
self.list_games.append(game['gamenumber'])
rows_to_delete = []
for game in self.list_games:
self.mycursor.execute(f"SELECT * FROM sc_log WHERE gnumber={game} ORDER BY 'remainingtop' DESC")
gameset = self.mycursor.fetchall()
rt = []
for rowdata in gameset:
if rowdata['remainingtop'] not in rt:
rt.append(rowdata['remainingtop'])
else:
rows_to_delete.append(rowdata)
for row in rows_to_delete:
print("Deleting: " + str(row))
# self.mycursor.execute("SHOW ENGINE INNODB STATUS")
self.mycursor.execute(f"DELETE FROM sc_log WHERE gnumber='{row['gnumber']}' AND "
f"remainingtop='{row['remainingtop']}' AND "
f"lastupdate='{row['lastupdate']}' AND "
f"datestarted='{row['datestarted']}';")
self.connection.commit()
def the_dead(self):
""" MOVE INFO FROM MAIN AND LOGS"""
""" TO RECORD HOW LONG CARDS SELL FOR"""
pass
m = Mover()
m.connect()
# m.main()
# m.cleaner()
# m.earliest()
m.duplicate_rt()
m.disconnect()