使用 shell 脚本从 sqlite 导出到 csv

发布于 2024-11-03 02:18:15 字数 466 浏览 0 评论 0 原文

我正在制作一个 shell 脚本来将 sqlite 查询导出到 csv 文件,就像这样:

 #!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db ".headers on"
./bin/sqlite3 ./sys/xserve_sqlite.db ".mode csv"
./bin/sqlite3 ./sys/xserve_sqlite.db ".output out.csv"
./bin/sqlite3 ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;"
./bin/sqlite3 ./sys/xserve_sqlite.db ".exit"

执行脚本时,输出出现在屏幕上,而不是保存到“out.csv”。它正在使用命令行执行相同的方法,但我不知道为什么 shell 脚本无法将数据导出到文件。

我做错了什么?

I'm making a shell script to export a sqlite query to a csv file, just like this:

 #!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db ".headers on"
./bin/sqlite3 ./sys/xserve_sqlite.db ".mode csv"
./bin/sqlite3 ./sys/xserve_sqlite.db ".output out.csv"
./bin/sqlite3 ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;"
./bin/sqlite3 ./sys/xserve_sqlite.db ".exit"

When executing the script, the output apears on the screen, instead of being saved to "out.csv". It's working doing the same method with the command line, but I don't know why the shell script fails to export data to the file.

What am I doing wrong?

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

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

发布评论

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

评论(7

月下伊人醉 2024-11-10 02:18:15

您可以使用 sqlite3 命令选项来代替点命令:

sqlite3 -header -csv my_db.db "select * from my_table;" > out.csv

这使其成为单行命令。

此外,您还可以运行 sql 脚本文件:

sqlite3 -header -csv my_db.db < my_script.sql > out.csv

使用 sqlite3 -help 查看可用选项的列表。

Instead of the dot commands, you could use sqlite3 command options:

sqlite3 -header -csv my_db.db "select * from my_table;" > out.csv

This makes it a one-liner.

Also, you can run a sql script file:

sqlite3 -header -csv my_db.db < my_script.sql > out.csv

Use sqlite3 -help to see the list of available options.

暗恋未遂 2024-11-10 02:18:15

sqlite3

每行都有一个对 sqlite3 的单独调用;当您的 select 运行时,您的 .out out.csv 已被遗忘。

尝试:

#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db <<!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!

相反。

sh/bash 方法

您可以使用重定向来调用脚本:

$ your_script >out.csv

也可以将以下内容插入脚本中作为第一行:

exec >out.csv

前一种方法允许您指定不同的文件名,而后者则输出到特定的文件名。在这两种情况下,.output out.csv 行都可以被忽略。

sqlite3

You have a separate call to sqlite3 for each line; by the time your select runs, your .out out.csv has been forgotten.

Try:

#!/bin/bash
./bin/sqlite3 ./sys/xserve_sqlite.db <<!
.headers on
.mode csv
.output out.csv
select * from eS1100_sensor_results;
!

instead.

sh/bash methods

You can either call your script with a redirection:

$ your_script >out.csv

or you can insert the following as a first line in your script:

exec >out.csv

The former method allows you to specify different filenames, while the latter outputs to a specific filename. In both cases the line .output out.csv can be ignored.

束缚m 2024-11-10 02:18:15

我最近创建了一个 shell 脚本,它将能够从 db 文件中获取表并将其转换为 csv 文件。

https://github.com/darrentu/convert-db-to-csv

如果对我的脚本有任何疑问,请随时问我:)

I recently created a shell script that will be able to take the tables from a db file and convert them into csv files.

https://github.com/darrentu/convert-db-to-csv

Feel free to ask me any questions on my script :)

枕梦 2024-11-10 02:18:15

虽然问题是关于 shell 脚本的,但我认为它会对那些只关心将数据从 sqlite3 数据库传输到 csv 文件的人有所帮助。

我找到了一种非常方便的方法,可以使用 SQLite Manager 扩展在 Firefox 浏览器中完成此操作。

只需在 Firefox 中连接到您的 sqlite 数据库文件(SQlite 管理器 -> 连接数据库),然后表 ->导出表。您将获得更多选项,您只需单击并尝试......

最后,您将获得一个 csv 文件,其中包含您选择导出的表。

Although the question is about shell script, I think it will help few of those who are just bothered about transferring the data from the sqlite3 database to a csv file.

I found a very convinient way to do it with the firefox browser using SQLite Manager extension.

Simply connect to your sqlite database file in firefox ( SQlite manager -> connect database ) and then Table -> Export table. You will be served with some more options that you can just click and try....

In the end you get a csv file with the table u have chosen to be exported.

海未深 2024-11-10 02:18:15
Using command line for Linux:

user@dell-Admin: sqlite3 #activate your sqlite database first
sqlite> .tables #search for tables if any available if already created one.
sqlite> .schema #if you want to check the schema of the table.

# once you find your table(s), then just do the following:

sqlite> .headers on   #export along with headers (column names)
sqlite> .mode csv     #file type is csv
sqlite> .output example.csv   #you want to provide file name to export
sqlite> SELECT * from events;    #If entire table is needed or select only required
sqlite> .quit    #finally quit the sqlite3

现在在您的系统中搜索 example.csv 文件,您就会得到它。

Using command line for Linux:

user@dell-Admin: sqlite3 #activate your sqlite database first
sqlite> .tables #search for tables if any available if already created one.
sqlite> .schema #if you want to check the schema of the table.

# once you find your table(s), then just do the following:

sqlite> .headers on   #export along with headers (column names)
sqlite> .mode csv     #file type is csv
sqlite> .output example.csv   #you want to provide file name to export
sqlite> SELECT * from events;    #If entire table is needed or select only required
sqlite> .quit    #finally quit the sqlite3

Now search in your system for example.csv file and you will get it.

我做我的改变 2024-11-10 02:18:15

在一行中是

sqlite3 -header -csv ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;" >./out.csv

In one line is

sqlite3 -header -csv ./sys/xserve_sqlite.db "select * from eS1100_sensor_results;" >./out.csv
长伴 2024-11-10 02:18:15

到目前为止答案的综合:

function sqlite2csv-table() {
    local db="${1}" table="${2}" output="${3}"
    if test -z "$output" ; then
        output="${db:r}_hi${table}.csv"
    fi
    [[ "$output" =~ '.csv

用法:

sqlite2csv some.db [/path/to/output]
]] || output+='.csv' echo "$0: outputting table '$table' to '$output'" sqlite3 -header -csv "$db" "select * from ${table};" > "$output" || return $? } function sqlite2csv() { local db="${1}" o="${2}" tables=($(sqlite3 $db ".tables")) local t for table in $tables[@] ; do sqlite2csv-table "$db" "$table" "${o}_${table}.csv" done }

用法:


A synthesis of the answers till now:

function sqlite2csv-table() {
    local db="${1}" table="${2}" output="${3}"
    if test -z "$output" ; then
        output="${db:r}_hi${table}.csv"
    fi
    [[ "$output" =~ '.csv

Usage:

sqlite2csv some.db [/path/to/output]
]] || output+='.csv' echo "$0: outputting table '$table' to '$output'" sqlite3 -header -csv "$db" "select * from ${table};" > "$output" || return $? } function sqlite2csv() { local db="${1}" o="${2}" tables=($(sqlite3 $db ".tables")) local t for table in $tables[@] ; do sqlite2csv-table "$db" "$table" "${o}_${table}.csv" done }

Usage:



    
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文