Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite3是否支持循环?_Sqlite - Fatal编程技术网

sqlite3是否支持循环?

sqlite3是否支持循环?,sqlite,Sqlite,我在下面编写了小bash脚本,它按预期工作,但为了可读性,我添加了一些注释和换行符,这破坏了代码。删除注释和换行符应使其成为有效脚本 ### read all measurements from the database and list each value only once sqlite3 -init /tmp/timeout /tmp/testje.sqlite \ 'select distinct measurement from errors order by measureme

我在下面编写了小bash脚本,它按预期工作,但为了可读性,我添加了一些注释和换行符,这破坏了代码。删除注释和换行符应使其成为有效脚本

### read all measurements from the database and list each value only once
sqlite3 -init /tmp/timeout /tmp/testje.sqlite \
  'select distinct measurement from errors order by measurement;' |

### remove the first line of stdout as this is a notification rather than intended output
sed '1d' |

### loop though all found values
while read error; do 

  ### count the number of occurences in the original table and print that
  sqlite3 -init /tmp/timeout /tmp/testje.sqlite \
    "select $error,count( measurement ) from errors where measurement = '$error' ;"
done
结果如下:

134         1                   
136         1                   
139         2                   
159         1
问题:是否可以使用
sqlite3
循环转换为SQL语句?换句话说,sqlite3是否支持某种类型的
-loop-to-loop遍历前一个查询的结果

现在我知道
sqlite3
是一个非常有限的数据库,很可能我想要的对它来说太复杂了。我一直在搜索,但我真的是一个数据库傻瓜,到目前为止,我得到的结果要么是在不同的数据库上,要么是在解决一个完全不同的问题


最简单的答案(顺便说一句,我不希望如此)是“sqlite3不支持循环”。

SQLite不支持循环。下面是一个例子,您会注意到结构化编程是完全不存在的

然而,这并不是说如果没有循环,使用集合或其他SQL构造就无法得到想要的东西。在您的情况下,它可能非常简单:

 select measurement, count( measurement ) from errors GROUP BY measurement
这将为您提供
错误
表中所有
测量
的列表,以及每个测量发生的频率计数


通常,SQL引擎的最佳利用方式是在单个(有时是复杂的)SQL语句中表达查询,并将其提交给引擎进行优化。在您的示例中,您已经编写了一些关于从数据库获取数据的策略的决策——SQL的宗旨是引擎比程序员更能做出这些决策。

SQLite不支持循环。下面是一个例子,您会注意到结构化编程是完全不存在的

然而,这并不是说如果没有循环,使用集合或其他SQL构造就无法得到想要的东西。在您的情况下,它可能非常简单:

 select measurement, count( measurement ) from errors GROUP BY measurement
这将为您提供
错误
表中所有
测量
的列表,以及每个测量发生的频率计数


通常,SQL引擎的最佳利用方式是在单个(有时是复杂的)SQL语句中表达查询,并将其提交给引擎进行优化。在您的示例中,您已经对用于从数据库获取数据的策略制定了一些决策——SQL的宗旨是,引擎比程序员更能做出这些决策。

这似乎是可行的!我没有意识到我可以将
GROUP
与count()结合使用。我已经习惯了把东西粘在一起,所以我写了一个小剧本。看起来很管用!我没有意识到我可以将
GROUP
与count()结合使用。我已经习惯了用胶水把东西粘在一起,所以我的小剧本。