-
Notifications
You must be signed in to change notification settings - Fork 7
/
readsql.py
68 lines (60 loc) · 1.95 KB
/
readsql.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
#coding=utf-8
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
import MySQLdb
class readdata(): #connect函数返回该数据库的链接
def __init__(self,db,passwd,user):
self.db=db
self.passwd="uiop.123.123"
self.user="root"
self.port=3306
def connectsql(self,):
try:
con=MySQLdb.connect(host="127.0.0.1",user=self.user,db=self.db,port=self.port,passwd=self.passwd,charset="utf8")
return con
except Exception as e:
print "got a error"
print str(e)
def retalldata(mycon): #返回数据库中的所有数据,格式为tuple
course=mycon.cursor()
try:
sql="select * from original_data_new order by OccurrenceTime"
course.execute(sql)
rs=course.fetchall()
return rs
except Exception as e:
print "get a error"
print str(e)
finally:
course.close()
def groupinfo(con):
course=con.cursor()
try:
infor={}
sql1="select NE from original_data_new group by NE "
sql2="select Name from original_data_new group by Name"
sql3="select AlarmSource from original_data_new group by AlarmSource"
course.execute(sql1)
infor['NE']=[x[0] for x in course.fetchall()]
course.execute(sql2)
infor['Name']=[x[0] for x in course.fetchall()]
course.execute(sql3)
infor['AlarmSource']=[x[0] for x in course.fetchall()]
return infor
except Exception as e:
print e
print str(e)
finally:
course.close()
if __name__=="__main__":
try:
database=readdata("test","uiop.123.123",'root')
con1=database.connectsql()
data=retalldata(con1)
print len(data)
#print data[2]
#infos=groupinfo(con1)
#print infos['AlarmSource']
finally:
con1.close()