使用触发器在 SQlite 中插入多行

Inserting multiple rows in SQlite using a trigger

我正在使用 SQlite,我已经 table1:

Count  | Fruit | Variety
------ |------ |--------
     2 | Pear  | Nashi
     3 | Plum  | Garnet

根据 table1 中的信息,我想使用查询以最佳方式生成 table2

Count  | Fruit | Variety
------ |------ |--------
     1 | Pear  | Nashi
     1 | Pear  | Nashi
     1 | Plum  | Garnet
     1 | Plum  | Garnet
     1 | Plum  | Garnet

我调查过的选项:

  1. 用户定义的函数 - 不可能,因为我现在无法更新二进制文件。否则,这是可能的,因为当我创建第一个时,我也可以在 C++ 中创建第二个 table。
  2. 简要介绍了触发器的使用,假设我可以允许用户将 (2, pear, Nashi) 之类的记录插入 table2,然后使用 INSTEAD OF 触发器插入基于计数的适当数量的记录。但是,看起来我不能在 table.
  3. 上使用 INSTEAD OF 触发器
  4. 研究过是否可以找到在 sqlite 中 运行 循环的方法,但看起来我的机会很渺茫。

可能有一个我不知道的更简单的解决方案。非常感谢任何帮助。

这可以通过递归触发器和 table2 (RCount) 中的附加列来计算剩余触发器的数量 运行:

PRAGMA recursive_triggers = ON;  -- must be executed in every connection

CREATE TRIGGER tr1
AFTER INSERT ON table1
BEGIN
    INSERT INTO table2(Count, Fruit, Variety, RCount)
    VALUES (1, NEW.Fruit, NEW.Variety, NEW.Count);
END;

CREATE TRIGGER tt2
AFTER INSERT ON table2
WHEN NEW.RCount > 1
BEGIN
    INSERT INTO table2(Count, Fruit, Variety, RCount)
    VALUES (1, NEW.Fruit, NEW.Variety, NEW.RCount - 1);
END;