-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb_connect.py
More file actions
46 lines (38 loc) · 1.16 KB
/
db_connect.py
File metadata and controls
46 lines (38 loc) · 1.16 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
import MySQLdb
'''https://github.com/mingli95/python-operational-monitoring'''
class Db(object):
def __init__(self,sql):
self.sql = sql
self.db = MySQLdb.connect("192.168.1.180",'liming','sbdscaaas123','yunwei')
self.cursor = self.db.cursor()
def Insert(self):
try:
self.cursor.execute(self.sql)
self.db.commit()
except:
self.db.rollback()
self.cursor.close()
self.db.close()
def update(self):
try:
self.cursor.execute(self.sql)
self.db.commit()
except:
self.db.rollback()
self.cursor.close()
self.db.close()
def Select(self):
self.cursor.execute(self.sql)
results = self.cursor.fetchall()
self.cursor.close()
self.db.close()
return results
def Create(self):
self.cursor.execute(self.sql)
self.db.commit()
self.cursor.close()
self.db.close()
if __name__ == '__main__':
data = """NSERT INTO yuwei.systeminfo (netname,address,hostname)
VALUES ('eno111','192.168.1.190','localhost.localhost')"""
Db(data).Insert()