当数据库里面的价格变化时,发送信息到企业微信中

发布时间:2020-08-13 14:49:29

来源:ITPUB博客

阅读:100

作者:czxin788

mysql insert 触发器

添加insert触发器,在insert一条新纪录时,当主单号不为空,并且新增价格和最近一次价格对比不相等时,说明价格有变化。这时触发器会自动将上一次老价格添加到当前新增行的unit_price_old老价格列。

这个需求是在一个表上,更新自己身上的其他列,这时需要用before,并且set new.列名,并且new必须在等号左边。

delimiter //

create trigger insert_flight_cabin_unit_price

before

insert on flight_cabin_book_o_update

for each row

begin

/*注意給变量赋值,等号右边的select必须加括号*/

/*获取每条主单号的最近一次更新时间*/

set @last_creat_time = (select creat from flight_cabin_book_o_update where waybill=new.waybill and flight_no=new.flight_no and flight_time=new.flight_time order by creat desc limit 1);

/*获取每条主单号的最近一次更新价格*/

set @last_unit_price = (select unit_price from flight_cabin_book_o_update where waybill=new.waybill and flight_no=new.flight_no and flight_time=new.flight_time and creat = @last_creat_time limit 1);

/*如果一个主单号不为空,并且最近一次价格和现在insert的价格不相同,就说明价格更新了*/

if new.waybill is not null and new.waybill !='' and new.unit_price != @last_unit_price then

set new.unit_price_old = @last_unit_price;

set new.unit_price_old_time = @last_creat_time;

end if;

end //

delimiter ;

mysql update触发器

这个是在一个表上update自己身上的其他列,也需要用before,并且

set new.列名,并且new必须在等号左边

delimiter //

create trigger update_flight_cabin_unit_price

before

update on czx

for each row

begin

if new.unit_price != old.unit_price then

set new.unit_price_old = old.unit_price;

set new.is_update_price = 'Y';

set new.update_price_time=now();

end if;

end //

delimiter ;

python脚本动态监听

#!/usr/bin/python

# -*- coding:utf8 -*-

# author: chenzhixin

from contextlib import contextmanager

import pymysql as mysqldb

import requests

import time

@contextmanager

def get_mysql_conn(**kwargs):

"""

建立MySQL数据库连接

:param kwargs:

:return:

"""

conn = mysqldb.connect(host=kwargs.get('host', 'localhost'),

user=kwargs.get('user'),

password=kwargs.get('password'),

port=kwargs.get('port', 3306),

database=kwargs.get('database')

)

try:

yield conn

finally:

if conn:

conn.close()

def execute_mysql_select_sql(conn, sql):

"""

执行mysql的select类型语句

:param conn:

:param sql:

:return:

"""

with conn as cur:

cur.execute(sql)

rows = cur.fetchall()

return rows

def execute_mysql_sql(conn, sql):

"""

执行mysql的dml和ddl语句,不包括select语句

:param conn:

:param sql:

:return:

"""

with conn as cur:

cur.execute(sql)

def get_mysql_flight_cabin_book_o_update_data(conn):

"""

获取 kb_kettle_data.flight_cabin_book_o_update的数据

:param conn:

:return:

"""

sql = "select " \

"id, " \

"waybill, " \

"flight_no," \

"flight_time," \

"unit_price, " \

"unit_price_old, " \

"unit_price_old_time," \

"creat " \

"from flight_cabin_book_o_update   " \

"where unit_price_old is not null"

mysql_table_rows = execute_mysql_select_sql(conn, sql)

if mysql_table_rows:

print('检测到价格变化:\n', mysql_table_rows)

for index, row in enumerate(mysql_table_rows, 1):

id = row[0]

waybill = row[1]

flight_no = row[2]

flight_time = row[3]

unit_price = row[4]

unit_price_old = row[5]

unit_price_old_time = row[6]

creat = row[7]

yield {'id': id,

'waybill': waybill,

'flight_no': flight_no,

'flight_time': flight_time,

'unit_price': unit_price,

'unit_price_old': unit_price_old,

'unit_price_old_time': unit_price_old_time,

'creat': creat

}

def send_to_qyweixin(dic):

"""

发送消息到企业微信

:param dic:

:return:

"""

headers = {"Content-Type": "text/plain"}

# s = "--石墨价格变化通知--\n主单号:{waybill}\n航班号:{flight_no}\n航班日期:{flight_time}\n\n时间1:{unit_price_old_time}\n-----------------------\n价格1:{unit_price_old}\n-----------------------\n{creat}\n主单号:{waybill}\n价格变为: {unit_price}".format(

#     waybill=dic['waybill'],

#     unit_price_old=dic['unit_price_old'],

#     unit_price_old_time=dic['unit_price_old_time'],

#     creat=dic['creat'],

#     unit_price=dic['unit_price']

# )

s = """

---石墨价格变化通知---

主单号:{waybill}

航班号:{flight_no}

航班日期:{flight_time}

时间1:{unit_price_old_time}

价格1:{unit_price_old}

时间2:{creat}

价格2:{unit_price}

""".format(

waybill=dic['waybill'],

flight_no=dic['flight_no'],

flight_time=dic['flight_time'],

unit_price_old=dic['unit_price_old'],

unit_price_old_time=dic['unit_price_old_time'],

creat=dic['creat'],

unit_price=dic['unit_price']

)

data = {

"msgtype": "text",

"text": {

"content": s,

}

}

r = requests.post(

url='https://qyapi.weixin.qq.com/cgi-bin/webhook/sexxxd5-eb13',

headers=headers, json=data)

print(r.text)

def main():

mysql_conn_args = dict(user='user1',

host='10.xx.xx.xx',

password='123456',

database='xxxxx')

with get_mysql_conn(**mysql_conn_args) as mysql_conn:

while True:

print('正在监听价格是否有变化.....')

# 1、首先获取mysql_flight_cabin_book_o_update表中有价格更新的所有行数据

mysql_data_dic = get_mysql_flight_cabin_book_o_update_data(mysql_conn)

# 2、其次遍历有价格变化的行数据,发送给企业微信

for dic in mysql_data_dic:

# 发送到企业微信

send_to_qyweixin(dic)

# 3、最后把mysql_flight_cabin_book_o_update表中的标记位is_update_price置为空

update_flag_sql = "update flight_cabin_book_o_update set unit_price_old=null,unit_price_old_time=null where waybill='{}'".format(dic['waybill'])

execute_mysql_sql(mysql_conn, update_flag_sql)

time.sleep(60)

if __name__ == '__main__':

main()

运行脚本

[root@gfs02 wangsn]# nohup python /usr/local/shell/monitor_price_qywx/monitor_price_to_qiyeweixin.py > /dev/null  2>&1 &

效果图

mysql当数据改变时_当数据库里面的价格变化时,发送信息到企业微信中相关推荐

  1. java数据库编程——元数据(metadata)+web 与企业应用中的连接管理

    [0]README 1) 本文部分文字描述转自 core java volume 2 , 测试源代码均为原创, 旨在理解 java数据库编程--元数据(metadata)+web 与企业应用中的连接管 ...

  2. mysql rpo是什么意思_揭开数据库RPO等于0的秘密(上)

    前言 传统商业关系数据库都声称可以做到故障恢复后不丢数据(即RPO为0),跟故障前的数据状态是强一致的,实际是否一定如此? 开源数据库MySQL在金融核心业务都不敢用,最重要的一个原因是做不到不丢数据 ...

  3. java导出数据透视表_使用数据库中的Java流制作数据透视表

    java导出数据透视表 来自数据库行和表的原始数据不能为人类读者提供太多了解. 相反,如果我们对数据执行某种聚合,则人类更有可能看到数据模式 在向我们展示之前. 数据透视表是聚合的一种特定形式,我们可 ...

  4. mac双系统装mysql启动不了了_详解ubuntu双系统启动时卡死解决办法

    详解ubuntu双系统启动时卡死解决办法 ubuntu双系统启动时卡死解决办法(在ubuntu16.04和18.04测试无误) 问题描述: 在安装完ubuntu双系统后,第一次启动ubuntu系统时, ...

  5. mysql 分库分表策略_【数据库】分库分表策略

    关系型数据库本身比较容易成为系统瓶颈,单机存储容量.连接数.处理能力都有限.当单表的数据量达到1000W或100G以后,由于查询维度较多,即使添加从库.优化索引,做很多操作时性能仍下降严重.此时就要考 ...

  6. mysql禁止数据被删除_为什么MySQL不建议delete删除数据

    我负责的有几个系统随着业务量的增长,存储在MySQL中的数据日益剧增,我当时就想现在的业务方不讲武德,搞偷袭,趁我没反应过来把很多表,很快,很快啊都打到了亿级别,我大意了,没有闪,这就导致跟其Join ...

  7. mysql字符集和表字符集_设置数据库的字符集和设置表字段字符集的区别是什么?...

    对于oracle来说,只有数据库字符集这个说法,不存在什么表字符集和字段字符集.你说的这个是mysql的字符集,数据库字符集可以和表字符集不同,也可以和列字符集不同,也就是说,你的数据库字符集为utf ...

  8. mysql添加数据不阻塞_主键表插入数据不提交,外键表插入数据被阻塞

    有客户和我说:他在含主外键的表中实验发现,在主表数据未提交,然后在外键表插入该数据数据时,出现外键表hang住现象.我开始以为是不同的会话,根据oracle数据库的一致性原则,应该新会话在外键表中不能 ...

  9. mysql 查看数据表大小_关于MySQL 查询表数据大小的总结

    一:关于MySQL表数据大小 我们知道mysql存储数据文件一般使用表空间存储 当mysql使用innodb存储引擎的时候,mysql使用表存储数据分为共享表空间和独享表空间两种方式 ·共享表空间:I ...

最新文章

  1. MapReduce源码分析之作业Job状态机解析(一)简介与正常流程浅析
  2. Oracle调优综述
  3. 别为了学编程而学编程
  4. 第九届蓝桥杯java B组—第三题复数幂(详细介绍)
  5. java localhosty_GitHub - yxxxd/sorryJava: 给动图加字幕的sorry项目的java版本
  6. yarn资源参数配置
  7. Django日志信息路径的设置
  8. angular语言前端开发_web前端开发入门全套学习方法路径,兼职在家做网站也能月入上万...
  9. 敏捷开发绩效管理之四:为团队设立外部绩效目标(目标管理,外向型绩效)...
  10. c语言变量生存期,C语言变量的生命周期
  11. c语言八数码A星算法代码解析,八数码问题c语言a星算法详细实验报告含代码解析...
  12. 百叶窗叶片锋利,不安全
  13. SSH三大框架的整合(实例)
  14. HttpGet请求数据乱码的原因
  15. Windows配置Rsync同步,安装cwRsync
  16. 树莓派上3g模块的使用
  17. uni-app设置背景图自适应手机屏幕尺寸
  18. React 16.x折腾记 - (7) 基于React+Antd封装聊天记录(用到React的memo,lazy, Suspense这些)
  19. oidc_使用OIDC和Ionic for JHipster保护您的移动应用程序
  20. html5 刮刮乐 源码,HTML5 canvas实现刮刮乐功能

热门文章

  1. 前端开发工程师如何在新的一年里提升自己
  2. oracle 存储过程定义及调试,并终于被C# 调用 代码
  3. 提升 DevOps 效率,试试 ChatOps 吧!
  4. C#——语言基础 之 运算符!
  5. centos 添加中文输入法
  6. 使用RSClientPrint直接打印本地RDLC报表
  7. eclipse 启动 找不到 JRE JDK的解决方法
  8. golang 检查ip地址格式 是否正确
  9. python 获取 文件修改时间 距离 当前时间 天数 秒数
  10. golang go get命令 一键获取代码、编译并安装