首页 > 解决方案 > SQLite/Room 中的“重复”功能

问题描述

我目前正在做我的一个副项目,我已经决定实施一个“记录/输入重复”系统。

所以基本上,在特定时间间隔内再次创建一行(但具有其他唯一 ID,因为每个费用都应该是唯一可编辑的)。例如:每月费用(例如租金)将是重复记录,如下所示:

费用

id, amount, date,      repId, status

1,   800,   01.03.2021, 1,    finished
2,   800,   01.04.2021, 1,    finished
3,   800,   01.05.2021, 1,    notpaidyet,
4,   800,   01.06.2021, 1,    notpaidyet 
5,   800,   01.07.2021, 1,    notpaidyet (=currentExpense/nextExpense, is always in the future!)

重复

id, repeatmenttype, numOfCreatedRepeatments, maxRepeatments,currentExpenseId
1,    MONTHLY,            5,                  unlimited,          5

我的想法是为此使用触发器,因为有很多情况我需要处理很多情况:

例如:

1)如果用户说他已经支付了让我们说 7 月 ( currentExpense) =UPDATE,我想自动创建 8 月的费用,只要numOfCreatedRepeatments< maxRepeatments

  1. 创建numOfCreatedRepeatments下一个时 自动更新。numOfCreatedRepeatments/currentExpense

3)如果currentExpense日期更改为 6 月 2 日(出于某种原因)并且repeatmenttype更改为 WEEKLY,则应创建新的费用记录直到当前日期 + 未来的额外费用(>currentDate),这将再次用作 currentExpense 所以该表将如下所示:

4,   800,   01.06.2021, 1,    notpaidyet 
5,   800,   02.06.2021, 1,    notpaidyet
6,   800,   09.06.2021, 1,    notpaidyet
7,   800,   16.06.2021, 1,    notpaidyet
8,   800,   23.06.2021, 1,    notpaidyet (=currentExpense, as 22.06 > 23.06!)

4)...

当 nextExpense 日期 = 当前日期时创建nextExpensenect 记录,将使用 解决AlarmManager,因此这将在代码中解决,因为 SQLite 不支持时间戳触发器。

我的问题是,最聪明的实现会是什么样子?

我的首选是使用触发器,但是我在某处读到触发器很慢,并且当 Expense 在其他表中用作外键时,我需要在外键表中创建额外的行。这会使触发器不可读并且不是很快,对吗?

另一方面,在代码中实现功能也会使代码有点不可读,并导致调用许多插入、更新等

标签: sqlitetriggersandroid-sqliteandroid-room

解决方案


推荐阅读