SQLite循环语句? [英] SQLite loop statements?

查看:962
本文介绍了SQLite循环语句?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在SQLite中是否有像 FOR .. in LOOP 之类的循环语句?我有两列 StartRange,EndRange ,我需要将整个序列插入到其他表中。因此,如果 StartRange 是1, EndRange 是3,则需要使用三个值来包含 1,2,3

解决方案

如果你有一个额外的表,它包含你所需要的所有整数,你可以在直接SQL中做这样的事情。假设你的 StartRange EndRange 范围在1到十,你有这样的表:

  sqlite>从ints中选择我; 

1



10

这个表格只包含您需要的所有可能的整数)。

然后如果你也有这样的话:

$ $ $ $ $ $ $ $ $ $ $ $ sqlite> create table t(startrange int not null,endrange int not null);
sqlite>插入t值(1,3);
sqlite>创建表目标(我不是空);

您可以将放入目标中一个连接:

 插入到目标(i)
从ints中选择ints.i
加入t on (ints.i> = t.startrange and ints.i< = t.endrange)

结果是这样的:

  sqlite>从目标选择*; 
i
1
2
3

你的真正的 t 会有更多的行,所以你想要一个WHERE子句来限制你看到的 t

类似的东西通常用日期来完成(查找日历表)。

所以如果你的范围很小(对于 small 的一些定义),然后生成一次 ints 表,并添加一个索引,并使用上述方法数据库中的所有INSERT。其他数据库有自己的方式(比如PostgreSQL的 generate_series )做这种事情,而不需要一个明确的 ints 表,但SQLite是(故意)有限。



SQL通常是基于集合的,因此循环并不自然。自然而然的是通过描述你所需要的来构建适当的集合。 OTOH,有时不自然的行为是必要和明智的。

我不知道这对您的应用程序是否有意义,我只是想我会演示如何完成。如果这种方法在你的情况下没有意义,那么你可以在数据库之外生成一堆INSERT语句。


Is there any loop statements in SQLite like FOR .. in .. LOOP or something like that? I have two columns StartRange, EndRange and I need to insert whole sequence in other table. So if StartRange is 1 and EndRange is 3 it's necessary to make three inserts with value, contains 1, 2, 3.

解决方案

You can do this sort of thing in straight SQL if you have an extra table that holds all the integers that you need.

Suppose your StartRange and EndRange range between one and ten and you have a table like this:

sqlite> select i from ints;
i
1
.
.
.
10

This table simply contains all the possible integers that you need (i.e. one through ten).

Then if you also have this:

sqlite> create table t (startrange int not null, endrange int not null);
sqlite> insert into t values(1, 3);
sqlite> create table target (i int not null);

You can do your INSERTs into target with a join:

insert into target (i)
select ints.i
from ints join t on (ints.i >= t.startrange and ints.i <= t.endrange)

The result is this:

sqlite> select * from target;
i
1
2
3

Of course your real t would have more rows so you'd want a WHERE clause to limit which row of t you look at.

Similar things are often done with dates (look up "calendar tables").

So if your ranges are small (for some definition of small) then generate your ints table once, add an index to it, and use the above technique to do all the INSERTs right inside the database. Other databases have their own ways (such as PostgreSQL's generate_series) to do this sort of thing without need an explicit ints table but SQLite is (intentionally) limited.

SQL is generally set-based so loops aren't natural. What is natural is building the appropriate sets by describing what you need. OTOH, sometimes unnatural acts are necessary and sensible.

I don't know if this makes sense for your application, I just thought I'd demonstrate how it can be done. If this approach doesn't make sense in your case then you can generate a bunch of INSERT statements outside the database.

这篇关于SQLite循环语句?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆