从数据库中获取数据并填充分区列表

时间:2019-06-26 09:16:18

标签: sql sqlite flutter dart sqflite

从前端角度以及从SQLite数据库查询数据,我对此都很困惑。如果您有任何解决方法,请回答。

SQLite数据库

我有一张像这样的桌子:

transactionId | productId | quantity
      1             2           1     
      2             4           0 
      3             1          null             
      4             3           1
      5             9           1             
      6             6           0
      7             1           1
      8             7           1
      9             8           1
     10             2           1
     11             0          null
     12             3           1
     13             5           1
     14             7           1
     15             1           0
     16             2           1
     17             9           1
     18             0          null
     19             2           1

现在,我要在flutter应用程序的列表中以5个单位的组(即直到5个单位完成的组)显示此数据。

所以第一组有8个项目,

第二个将有6个项目,

第3组将有5个项目 (并且仍不完整,因为可以添加更多项目,直到该组的数量变为5)

类似这样的东西:

enter image description here

现在,我的应用可以具有多个这样的组。另外,我不认为Grid View Builder可以在这里工作,因为对于每个组,我必须显示该组的一些数据以及累积的数据(图中未显示)

问题:

1)如何从SQFLite数据库查询数据?

2)如何在Flutter App前端显示查询的数据?

1 个答案:

答案 0 :(得分:1)

不幸的是,这种类型的问题需要递归CTE(或其他迭代处理)。

假设transactionId是连续的且没有空格:

with recursive cte as (
      select transactionId, productId,
             coalesce(quantity, 0) as quantity,
             1 as bin
      from t
      where transactionId = 1
      union all
      select t.transactionId, t.productId,
             (case when cte.quantity > 5
                   then 0 else cte.quantity
              end)  + coalesce(t.quantity, 0) as quantity,
             (case when cte.quantity > 5 then 1 else 0 end) + cte.bin as bin
      from cte join
           t
           on t.transactionId = cte.transactionId + 1
     )
select *
from cte;

如果transactionId有差距或其他问题,只需使用row_number()(在另一个CTE中)为where子句创建适当的列即可。

相关问题