-
Notifications
You must be signed in to change notification settings - Fork 4
/
save_to_sql.py
195 lines (174 loc) · 7.15 KB
/
save_to_sql.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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
# _*_ coding:UTF-8 _*_
'''
Created on Mar 7, 20172:06:13 PM
Author: VIC
Email: victor.wei@msn.cn
Link: http://www.weizhikai.com
'''
import pymysql
import config
from pymysql import cursors
class sqloperater:
def __init__(self, sqlstr):
super().__init__()
self.sqlstr = sqlstr
self.status = ''
# drop existed table
def drop_table(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(self.sqlstr)
connection.commit()
self.status = 'Table dropped Sucessfully'
except Exception as e:
self.status = 'Failed to drop Table', str(e)
# create new table
def create_table(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(self.sqlstr)
connection.commit()
self.status = 'Table created Sucessfully'
except Exception as e:
self.status = 'Failed to Create Table', str(e)
# add data to table
def AddData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
cursor.execute(self.sqlstr)
connection.commit()
self.status = 'Added Successfully'
except Exception as e:
self.status = 'Failed to Add', str(e)
finally:
connection.close()
# bulk-action for adding data to table
def AddManyData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor)
try:
with connection.cursor() as cursor:
cursor.executemany(self.sqlstr)
connection.commit()
self.status = 'Added Successfully'
except Exception as e:
self.status = 'Failed to Add', str(e)
finally:
connection.close()
# update data in the table
def updateData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(sqlstr)
connection.commit()
self.status = 'Updated Successfully'
except Exception as e:
self.status = 'Failed to Update', str(e)
finally:
connection.close()
# delete data from the table
def deleteData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(sqlstr)
connection.commit()
self.status = 'Deleted Successfully'
except Exception as e:
self.status = 'Failed to Delete ', str(e)
finally:
connection.close()
# read data from table
def fetchOneData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(sqlstr)
self.sqldata = cursor.fetchone()
self.status = 'Fetch Data Successfully'
except Exception as e:
self.status = 'Failed to Fetch Data ', str(e)
self.sqldata = []
finally:
connection.close()
# read all data from table
def fetchAllData(self, sqlstr):
connection = pymysql.connect(host=config.HOST,
user=config.USER,
password=config.PASSWORD,
db=config.DATABASE,
charset=config.CHARSET,
cursorclass=pymysql.cursors.DictCursor
)
try:
with connection.cursor() as cursor:
cursor.execute(sqlstr)
self.sqldata = cursor.fetchall()
self.status = 'Fetch Data Successfully'
except Exception as e:
self.status = 'Failed to Fetch Data ', str(e)
self.sqldata = []
finally:
connection.close()
# retrun execution status
def get_status(self):
return self.status
# return data
def get_data(self):
return self.sqldata
# read and save data to a file
def sql_to_set(self, sqlstr):
sqlreader = sqloperater(sqlstr)
sqlreader.fetchAllData(sqlstr)
data = sqlreader.get_data()
for i in range(len(data)):
print(data[i])
'''
sqlstr = "delete from `aepro`.`db_aecategories`;"
saver = sqloperater(sqlstr)
saver.deleteData(sqlstr)
print(saver.get_status())
'''