逐行解析sqlite查询

发布于 2024-10-31 05:25:28 字数 273 浏览 0 评论 0原文

我有一个我自己无法解决的问题,我有一个包含数据的 SQLite 数据库 要检索数据,我使用 bash 脚本和命令 sqlite3 db "SELECT prkey FROM Printers"

输出如下:

1

3

4

5

6

7

我需要解析每一行并将其用于我的下一个命令。并且无需使用 >; file

问候 Marco

i have a problem which i cant figure out myself, i have a SQLite database which contains data
to retrieve the data i use a bash script and the command sqlite3 db "SELECT prkey FROM printers"

the output is something like this:

1

3

4

5

6

7

i need to parse each line and use it for my next command. and with out the use of a > file

regards Marco

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

总以为 2024-11-07 05:25:28

for 循环可以工作,但它们不是很健壮:如果输出中的一行有空格,它们就会中断,并且 shell 在开始运行循环之前必须将查询的整个输出存储在内存中。这两个问题很容易解决:

sqlite3 db "SELECT ..." | while read prkey; do
   echo "do stuff with $prkey"
done

The for loops work, but they are not very robust: they break if a line in the output has a space, and the shell must store the whole output of the query in memory before it starts running the loop. These two issues are easily dealt with:

sqlite3 db "SELECT ..." | while read prkey; do
   echo "do stuff with $prkey"
done
○闲身 2024-11-07 05:25:28
#!/bin/bash

for line in $(sqlite3 db "SELECT prkey FROM printers"); do
  echo "line is --> $line" # do stuff with $line
done
#!/bin/bash

for line in $(sqlite3 db "SELECT prkey FROM printers"); do
  echo "line is --> $line" # do stuff with $line
done
油焖大侠 2024-11-07 05:25:28

您可以对 sqlite 命令的返回值使用 for 循环。

for prkey in `sqlite3 db "SELECT prkey FROM printers"`; do
    echo $prkey; #replace with your command
done;

You can use for loop on the return value of the sqlite command.

for prkey in `sqlite3 db "SELECT prkey FROM printers"`; do
    echo $prkey; #replace with your command
done;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文