业务前提 : 我在实际工作中有这样一项任务 , 等待数据库中新增债券代码的完成

这些代码一般都是新发行的债券

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。

加代码任务不需要我负责 , 我需要监控一下代码的添加完成情况

主要是定时执行SQL脚本 , 比较麻烦

所以有如下python脚本

 

 1 import time,pymssql
 2 def mytask(): #循环执行的任务
 3     conn = pymssql.connect('192.168.xxx.xxx','read', 'read', 'fcdb') #服务器地址 , 登录账号 , 密码 ,数据库名称
 4     cursor = conn.cursor()
 5     symbol="('156911','152123')" #需要监控的债券代码
 6     sql=("select symbol,exchange,b.entrydate,b.entrytime,bondissue12,bondissue13 from dbview_bondname a, bonddt b ,bondissue c"
 7     +" where a.bcode=b.bcode and a.bcode=c.bcode and symbol in"+symbol) #通过查询语句查看代码添加的完成情况
 8     cursor.execute(sql)
 9     cnt = 0
10     for row in cursor:
11         print("代码市场:",row[:2])
12         print("录入时间:",row[2:4])
13         print("发行起止:",row[4:])
14         cnt += 1
15     conn.close()
16     return cnt
17 def re_exe(n): 
18     cnt=mytask()
19     while not cnt:
20         print(cnt)
21         cnt=mytask()    
22         time.sleep(n)
23     print("代码已经同步好了%d条" % cnt)
24 if __name__=='__main__':
25     re_exe(120) #设置循环执行的时间 单位是秒

  python脚本->定时连接数据库执行指定SQL语句 随笔

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄