我寫了下面的小bash腳本,它按預期工作,但我添加了一些註釋和換行代碼,以便讀取代碼。刪除註釋和換行應該使其成爲有效的腳本。`sqlite3`支持循環嗎?
### 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
翻譯的while
-loop到SQL語句?換句話說,sqlite3是否支持某種for
-loop循環查看先前查詢的結果?
現在我知道sqlite3
是一個非常有限的數據庫,可能是我想要的只是太複雜了。我一直在尋找,因爲它,但我真的是一個數據庫nitwit,到目前爲止我得到的結果要麼在不同的數據庫上,要麼解決完全不同的問題。
最簡單的答案(我不希望順便說一句)是'sqlite3不支持循環'。
這似乎工作!我沒有意識到我可以將'GROUP'與count()結合使用。我非常習慣於把東西拼湊在一起,因此我的小腳本。 – jippie