Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/shell/5.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
使用shell脚本从sqlite导出到csv_Sqlite_Shell_Csv - Fatal编程技术网

使用shell脚本从sqlite导出到csv

使用shell脚本从sqlite导出到csv,sqlite,shell,csv,Sqlite,Shell,Csv,我正在制作一个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 "selec

我正在制作一个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脚本无法将数据导出到文件中

我做错了什么?

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

尝试:

或者,您可以在脚本中插入以下内容作为第一行:

exec >out.csv

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

可以使用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-header-csv my_db.dbout.csv

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

我最近创建了一个shell脚本,可以从db文件中获取表格并将其转换为csv文件


请随时问我有关脚本的任何问题:)

尽管问题是关于shell脚本的,但我认为它将帮助那些只关心将数据从sqlite3数据库传输到csv文件的人

我发现了一种非常方便的方法,使用SQLite管理器扩展实现firefox浏览器

只需连接到firefox中的sqlite数据库文件(sqlite管理器->连接数据库),然后连接表->导出表。我们将为您提供更多选项,您只需单击并尝试

最后,您将得到一个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
现在在您的系统中搜索例如.csv文件,您将得到它

一行是

sqlite3-header-csv./sys/xserve_sqlite.db“从eS1100_传感器结果中选择*”>。/out.csv

迄今为止答案的综合:

function sqlite2csv-table() {
    local db="${1}" table="${2}" output="${3}"
    if test -z "$output" ; then
        output="${db:r}_hi${table}.csv"
    fi
    [[ "$output" =~ '.csv$' ]] || 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
}
用法:

sqlite2csv some.db [/path/to/output]

太好了,谢谢,现在开始工作了!(需要“>>”而不是“>”,但我是linux中的新手:D)如果这里的答案确实回答了您的问题,您应该单击答案总票数下方的复选标记(您应该会看到浅灰色)。这个问题已经回答了。抱歉耽搁了,我是一个真正的新手:D
好多了。。。这应该是公认的解决方案!KISS:-)如果sqlite3位于您的路径上或同一目录中,此解决方案也可以在windows上运行。这对我很有帮助。您应该将10行代码复制粘贴到您的答案中,这样我已经给出的upvote就更有道理了。脚本缺少表名周围的引号(如果表名有破折号,则会引发错误)
function sqlite2csv-table() {
    local db="${1}" table="${2}" output="${3}"
    if test -z "$output" ; then
        output="${db:r}_hi${table}.csv"
    fi
    [[ "$output" =~ '.csv$' ]] || 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
}
sqlite2csv some.db [/path/to/output]