使用SQL计算宝宝每次吃奶的时间间隔(续)

本文是《使用SQL计算宝宝每次吃奶的时间间隔》的续篇,因为我工作繁忙,时常不能及时帮助媳妇儿记录,为了让不懂数据库的媳妇儿也可以自己用手机熟练操作。我继续做了一些修正和封装:

  • 1.给媳妇儿的手机下载一个terminal终端软件termius,实现只需打开app就可以连接到环境;
  • 2.跟媳妇儿进一步沟通需求,按她能够接受的使用习惯来修正并封装程序,原则是尽可能的简化操作。

先来看下改进后最终的使用效果:

--1.手机上打开termius软件,点击进入到终端,输入 h 可以看到所有命令的说明:
[oracle@jystdrac1 ~]$ h
**Command for Baby's feed_time:**
Usage:
 v                      - View Today's Result.
 vv <'mmdd'>            - View one day's Result.
 i                      - Insert a row using current time.
 ii <'mmdd hh24:mi'>    - Insert a row using input time.
 d                  - Delete a mistake row by id. 
 u 

经过一番演示,媳妇儿的反馈是非常满意的,实际她最常用的还是v和i,非常方便,其他命令偶尔使用,其实只需记住h可以获取到帮助即可。下面将本次的修正和封装过程记录一下:

  • 1.环境变量配置别名
  • 2.系统shell脚本
  • 3.底层SQL文本

1.环境变量配置别名

为了简化操作,我将环境变量设置一些别名。

[oracle@jystdrac1 ~]$ cat ~/.bash_profile 
# 这里省略原有环境变量内容,下面为在源文件基础上追加的内容:
# Alias for baby's feed_time:
alias h=/home/oracle/baby_readme.sh
alias v=/home/oracle/baby_view.sh
alias i=/home/oracle/baby_insert.sh
alias d=/home/oracle/baby_delete.sh
alias u=/home/oracle/baby_update.sh
alias ii=/home/oracle/baby_insert_diy.sh
alias vv=/home/oracle/baby_view_diy.sh

2.系统shell脚本

baby_readme.sh

vi baby_readme.sh

#!/bin/bash
# script:baby_readme.sh
# version:1.01
#-------------------------------------------------------------
echo "**Command for Baby's feed_time:**"
echo "Usage:"
echo " v            - View Today's Result."
echo " vv <'mmdd'>      - View one day's Result."
echo " i            - Insert a row using current time."
echo " ii <'mmdd hh24:mi'>  - Insert a row using input time."
echo " d        - Delete a mistake row by id."
echo " u 

baby_view.sh

vi baby_view.sh

#!/bin/bash
# script:baby_view.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

baby_insert.sh

vi baby_insert.sh

#!/bin/bash
# script:baby_insert.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

baby_delete.sh

vi baby_delete.sh

#!/bin/bash
# script:baby_delete.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

baby_insert_diy.sh

vi baby_insert_diy.sh

#!/bin/bash
# script:baby_insert_diy.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

baby_update.sh

vi baby_update.sh

#!/bin/bash
# script:baby_update.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

baby_view_diy.sh

vi baby_view_diy.sh

#!/bin/bash
# script:baby_view.sh
# version:1.01
#-------------------------------------------------------------
sqlplus -S test/test <

3.底层SQL文本

--echo "v - View Today's Result."
[oracle@jystdrac1 ~]$ cat v3.sql
select id, to_char(feed_time,'mm-dd hh24:mi') "FEED_TIME", label, round((feed_time - l_time) * 24 * 60) "LAG(min)", round((feed_time - l_time) * 24, 2) "LAG(h)"
from (select t.*, lag(feed_time) over(order by id) l_time from t_baby t where label <> 'L') where feed_time >= trunc(sysdate)
/

-- echo " vv <'mmdd'>       - View one day's Result."
[oracle@jystdrac1 ~]$ cat v4.sql 
select id, to_char(feed_time,'mm-dd hh24:mi') "FEED_TIME", label, round((feed_time - l_time) * 24 * 60) "LAG(min)", round((feed_time - l_time) * 24, 2) "LAG(h)"
from (select t.*, lag(feed_time) over(order by id) l_time from t_baby t where label <> 'L') where feed_time >= to_date('&feed_time','mmdd') and feed_time < to_date('&feed_time','mmdd') + 1
/

--echo "i - Insert a row using current time."
[oracle@jystdrac1 ~]$ cat i1.sql 
insert into t_baby(id,feed_time) values(s1.nextval,sysdate)
/
commit
/

--echo "ii - Insert a row using input time."
[oracle@jystdrac1 ~]$ cat i2.sql 
insert into t_baby(id,feed_time) values(s1.nextval,to_date('&feed_time','mmdd hh24:mi'))
/

--echo "d - Delete a mistake row by id. "
[oracle@jystdrac1 ~]$ cat d1.sql 
delete from t_baby where id = &id
/

--echo "u - Update a mistake row by id."
[oracle@jystdrac1 ~]$ cat u1.sql 
update t_baby set label = upper('&label') where id = &id
/

SQL文本独立出来也方便后续需求有变化时快速更改。

你可能感兴趣的:(使用SQL计算宝宝每次吃奶的时间间隔(续))