-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmysql2a.py
More file actions
61 lines (54 loc) · 1.57 KB
/
mysql2a.py
File metadata and controls
61 lines (54 loc) · 1.57 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
50
51
52
53
54
55
56
57
58
59
60
61
import MySQLdb
import traceback
import time
class Mysql:
def __init__(self, args):
self.ip = args["ip"]
self.user = args["user"]
self.passwd = args["pwd"]
self.db = args['db']
self.table = args["table"]
self.values = {}
self.connect()
def connect(self):
self.database = MySQLdb.connect(self.ip, self.user, self.passwd, self.db)
self.cursor = self.database.cursor()
def run_fetch(self, cmd):
try:
self.cursor.execute(cmd)
self.database.commit()
result = self.cursor.fetchall()
return result
except Exception:
#print(traceback.format_exc())
time.sleep(1)
self.close()
self.connect()
self.cursor.execute(cmd)
self.database.commit()
result = self.cursor.fetchall()
return result
def close(self):
self.cursor.close()
self.database.close()
if __name__ == '__main__':
mysql_args = {
"ip":'127.0.0.1',
"user":'graymatics',
"pwd":'graymatics',
"db":'test0',
"table":"test1",
#"column": [
# #['c1','INT NOT NULL AUTO_INCREMENT, primary key(c1)'],
# ['c1','INT NOT NULL'],
# ['c2','varchar(40)']
# ]
}
mysql = Mysql(mysql_args)
#for i in range(100):
# mysql.insert_fast([12345, 'abcde'])
#mysql.commit_insert()
t0 = time.time()
for i in range(1):
print(mysql.fetch(['c2,c1']))
print(time.time() -t0)