1
1
import time
2
+ import munch
2
3
import mysql .connector
3
4
from datetime import datetime
4
5
from mysql .connector import errorcode
@@ -10,11 +11,27 @@ class DataBase:
10
11
def __init__ (self , config ):
11
12
self .cnx = mysql .connector .connect (** config .connection )
12
13
self .database_name = config .database_name
13
- self .tables = config .tables
14
- self .table_description = config .table_description
15
- self .insert_description = config .insert_description
16
- self .select_description = config .select_description
14
+ self .table_desc = "CREATE TABLE `{}` " \
15
+ "(`index` int NOT NULL, " \
16
+ "`start_time` timestamp(6) NOT NULL," \
17
+ "`end_time` timestamp(6) NOT NULL, " \
18
+ "`result` TEXT, " \
19
+ "`log` VARCHAR(255)," \
20
+ "PRIMARY KEY(`index`)) ENGINE=InnoDB"
17
21
22
+ self .insert_desc = "INSERT INTO `{}` " \
23
+ "(`index`, `start_time`, `end_time`, `result`, `log`) " \
24
+ "VALUES (%s ,%s, %s, %s, %s);"
25
+
26
+ self .select_desc = "SELECT `index`, `start_time`, `end_time`, `result`, `log` FROM `{}`;"
27
+
28
+ self .select_one_desc = "SELECT `index`, `start_time`, `end_time`, `result`, `log` FROM `{}`" \
29
+ "where `index` = %s"
30
+
31
+ self .update_desc = "UPDATE {} SET `end_time` = %s, " \
32
+ "`result` = %s, " \
33
+ "`log` = %s " \
34
+ "WHERE `index` = %s"
18
35
19
36
def _create_database (self , cursor ):
20
37
try :
@@ -38,29 +55,27 @@ def use_database(self):
38
55
exit (1 )
39
56
cursor .close ()
40
57
41
- def create_tables (self ):
58
+ def create_table (self , edge_id ):
42
59
cursor = self .cnx .cursor ()
43
- table_description = self .table_description
44
- for table_name in self .tables :
45
- try :
46
- logger .info ("Creating table {}: " .format (table_name ))
47
- cursor .execute (table_description .format (table_name ))
48
- except mysql .connector .Error as err :
49
- if err .errno == errorcode .ER_TABLE_EXISTS_ERROR :
50
- cursor .execute ("drop table {}" .format (table_name ))
51
- logger .info ("already exists, drop it." )
52
- cursor .execute (table_description .format (table_name ))
53
- else :
54
- logger .error (err .msg )
60
+ table_sql = self .table_desc
61
+ try :
62
+ logger .info ("Creating table {}: " .format (edge_id ))
63
+ cursor .execute (table_sql .format (edge_id ))
64
+ except mysql .connector .Error as err :
65
+ if err .errno == errorcode .ER_TABLE_EXISTS_ERROR :
66
+ cursor .execute ("drop table {}" .format (edge_id ))
67
+ logger .info ("already exists, drop it." )
68
+ cursor .execute (table_sql .format (edge_id ))
55
69
else :
56
- logger .success ("create successfully" )
70
+ logger .error (err .msg )
71
+ else :
72
+ logger .success ("create successfully" )
57
73
cursor .close ()
58
74
59
75
def clear_table (self , edge_id ):
60
76
cursor = self .cnx .cursor ()
61
- table_name = self .tables [edge_id - 1 ]
62
77
try :
63
- cursor .execute ("truncate table {}" .format (table_name ))
78
+ cursor .execute ("truncate table {}" .format (edge_id ))
64
79
except mysql .connector .Error as err :
65
80
cursor .close ()
66
81
logger .error (err .msg )
@@ -71,10 +86,9 @@ def clear_table(self, edge_id):
71
86
72
87
def select_result (self , edge_id ):
73
88
cursor = self .cnx .cursor ()
74
- select_description = self .select_description
75
- table_name = self .tables [edge_id - 1 ]
89
+ select_sql = self .select_desc
76
90
try :
77
- cursor .execute (select_description .format (table_name ))
91
+ cursor .execute (select_sql .format (edge_id ))
78
92
results = cursor .fetchall ()
79
93
except Exception as e :
80
94
logger .error ('Query failed {}' .format (e ))
@@ -85,9 +99,24 @@ def select_result(self, edge_id):
85
99
logger .success ('query successfully' )
86
100
return results
87
101
102
+ def select_one_result (self , edge_id , index ):
103
+ cursor = self .cnx .cursor ()
104
+ select_sql = self .select_one_desc
105
+ try :
106
+ cursor .execute (select_sql .format (edge_id ), index )
107
+ result = cursor .fetchone ()
108
+ except Exception as e :
109
+ logger .error ('Query failed {}' .format (e ))
110
+ cursor .close ()
111
+ return None
112
+ else :
113
+ cursor .close ()
114
+ logger .success ('query successfully' )
115
+ return result
116
+
88
117
def insert_data (self , table_name , data ):
89
118
cursor = self .cnx .cursor ()
90
- insert_sql = self .insert_description .format (table_name )
119
+ insert_sql = self .insert_desc .format (table_name )
91
120
try :
92
121
cursor .execute (insert_sql , data )
93
122
# Make sure data is committed to the database
@@ -101,6 +130,27 @@ def insert_data(self, table_name, data):
101
130
logger .success ('insert successfully' )
102
131
103
132
133
+ def update_data (self , table_name , data ):
134
+ cursor = self .cnx .cursor ()
135
+ update_sql = self .update_desc .format (table_name )
136
+ try :
137
+ cursor .execute (update_sql , data )
138
+ # Make sure data is committed to the database
139
+ self .cnx .commit ()
140
+
141
+ except Exception as e :
142
+ cursor .close ()
143
+ logger .error ("update error {}" .format (e ))
144
+ else :
145
+ cursor .close ()
146
+ logger .success ('update successfully' )
147
+
148
+
104
149
105
150
if __name__ == '__main__' :
106
- pass
151
+ config = {
152
+ "connection" : {'user' : 'root' , 'password' : 'root' , 'host' : '127.0.0.1' , 'raise_on_warnings' : True },
153
+ "database_name" : 'mydatabase' ,
154
+ }
155
+ config = munch .munchify (config )
156
+ mydb = DataBase (config )
0 commit comments