forked from lazy20017/python_TCPserver_Thread
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path2usersqlserver.py
More file actions
78 lines (66 loc) · 2.64 KB
/
2usersqlserver.py
File metadata and controls
78 lines (66 loc) · 2.64 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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
import pymssql
import numpy as np
#import pylab as pl
import matplotlib # 注意这个也要import一次
import matplotlib.pyplot as pl
import matplotlib.font_manager as fm
myfont = matplotlib.font_manager.FontProperties(fname=r'C:/Windows/Fonts/msyh.ttf')
print("使用mssqlserver的方法1")
class MSSQL:
def __init__(self,host,user,pwd,db): #类的构造函数,初始化数据库连接ip或者域名,以及用户名,密码,要连接的数据库名称
self.host=host
self.user=user
self.pwd=pwd
self.db=db
def __GetConnect(self): #得到数据库连接信息函数, 返回: conn.cursor()
if not self.db:
rasie(NameError,"没有设置数据库信息")
self.conn=pymssql.connect(host=self.host,user=self.user,password=self.pwd,database=self.db,charset='utf8')
cur=self.conn.cursor() #将数据库连接信息,赋值给cur。
if not cur:
raise(NameError,"连接数据库失败")
else:
return cur
#执行查询语句,返回的是一个包含tuple的list,list的元素是记录行,tuple的元素是每行记录的字段
def ExecQuery(self,sql): #执行Sql语句函数,返回结果
cur = self.__GetConnect() #获得数据库连接信息
cur.execute(sql) #执行Sql语句
resList = cur.fetchall() #获得所有的查询结果
#查询完毕后必须关闭连接
self.conn.close() #返回查询结果
return resList
def ExecNonQuery(self,sql):
cur = self.__GetConnect()
cur.execute(sql)
self.conn.commit()
self.conn.close()
def main():
ms=MSSQL(host="106.14.41.25:3539",user="sa",pwd="NcisT.DKyT_123456",db="DTU_SERVER_NEW") #实例化类对象,连接数据对象
reslist =ms.ExecQuery("SELECT TOP(3) * FROM DTU_CYCLE_DATA_VIEW2 WHERE DTU编号 = '65001' order by id DESC")
for id in reslist: #遍历返回结果
print(id) #转换为字符串,打印出来。
print(type(reslist))
a=reslist[0]
print(a)
#for x in a:
#print(x)
reslist = ms.ExecQuery("SELECT TOP(300) B相温度 FROM DTU_CYCLE_DATA_VIEW2 WHERE DTU编号 = '65001' order by id DESC")
#for id in reslist: #遍历返回结果
#print(id) #转换为字符串,打印出来。
print(type(reslist))
y=reslist
x2=[]
for i in y:
if i[0]>50:
print(i[0])
elif i[0]>0:
x2.append(float(i[0]))
else:
print(i[0])
print(x2)
count=len(x2)
print(count)
t = np.arange(0, count, 1)
pl.plot(t, x2, label="正弦曲线")
pl.show()
main()