SQLite:如何将查询结果保存为 CSV 文件?

发布于 2024-11-08 14:11:12 字数 31 浏览 0 评论 0原文

有没有办法可以将查询结果导出到 CSV 文件中?

Is there a way I can export the results of a query into a CSV file?

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

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

发布评论

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

评论(7

你的往事 2024-11-15 14:11:12

来自 此处 和 d5e5 的评论:

您必须将输出切换到 csv 模式并切换到文件输出。

sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout

From here and d5e5's comment:

You'll have to switch the output to csv-mode and switch to file output.

sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout
溺渁∝ 2024-11-15 14:11:12

要将列名称包含到 csv 文件中,您可以执行以下操作:

sqlite> .headers on
sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout

要验证您所做的更改,您可以运行以下命令:

sqlite> .show

输出:

echo: off   
explain: off   
headers: on   
mode: csv   
nullvalue: ""  
output: stdout  
separator: "|"   
stats: off   
width: 22 18 

To include column names to your csv file you can do the following:

sqlite> .headers on
sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout

To verify the changes that you have made you can run this command:

sqlite> .show

Output:

echo: off   
explain: off   
headers: on   
mode: csv   
nullvalue: ""  
output: stdout  
separator: "|"   
stats: off   
width: 22 18 
仙气飘飘 2024-11-15 14:11:12

除了上述答案之外,您还可以以与 .output 类似的方式使用 .once。这仅将下一个查询输出到指定的文件,因此您不必遵循.output stdout

所以在上面的例子中

.mode csv
.headers on
.once test.csv
select * from tbl1;

In addition to the above answers you can also use .once in a similar way to .output. This outputs only the next query to the specified file, so that you don't have to follow with .output stdout.

So in the above example

.mode csv
.headers on
.once test.csv
select * from tbl1;
雪若未夕 2024-11-15 14:11:12

或者,您可以在一行中完成(在 win10 中测试)

sqlite3 -help
sqlite3 -header -csv db.sqlite 'select * from tbl1;' > test.csv

额外奖励:使用带 cmdlet 和管道 (|) 的 powershell。

get-content query.sql | sqlite3 -header -csv db.sqlite > test.csv

其中 query.sql 是包含 SQL 查询的文件

Alternatively you can do it in one line (tested in win10)

sqlite3 -help
sqlite3 -header -csv db.sqlite 'select * from tbl1;' > test.csv

Bonus: Using powershell with cmdlet and pipe (|).

get-content query.sql | sqlite3 -header -csv db.sqlite > test.csv

where query.sql is a file containing your SQL query

悟红尘 2024-11-15 14:11:12

gdw2 和 d5e5 给出了很好的答案。为了使其更简单,这里将建议集中在一系列命令中:

sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout

Good answers from gdw2 and d5e5. To make it a little simpler here are the recommendations pulled together in a single series of commands:

sqlite> .mode csv
sqlite> .output test.csv
sqlite> select * from tbl1;
sqlite> .output stdout
感性 2024-11-15 14:11:12

所有现有的答案只能在 sqlite 命令行中使用,如果您想构建可重用的脚本,这并不理想。 Python 可以轻松构建可以编程执行的脚本。

import pandas as pd
import sqlite3

conn = sqlite3.connect('your_cool_database.sqlite')

df = pd.read_sql('SELECT * from orders', conn)
df.to_csv('orders.csv', index = False)

您可以自定义查询以仅将部分 sqlite 表导出到 CSV 文件。

您还可以运行单个命令将所有 sqlite 表导出到 CSV 文件:

for table in c.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall():
    t = table[0]
    df = pd.read_sql('SELECT * from ' + t, conn)
    df.to_csv(t + '_one_command.csv', index = False)

请参阅 此处了解更多信息。

All the existing answers only work from the sqlite command line, which isn't ideal if you'd like to build a reusable script. Python makes it easy to build a script that can be executed programatically.

import pandas as pd
import sqlite3

conn = sqlite3.connect('your_cool_database.sqlite')

df = pd.read_sql('SELECT * from orders', conn)
df.to_csv('orders.csv', index = False)

You can customize the query to only export part of the sqlite table to the CSV file.

You can also run a single command to export all sqlite tables to CSV files:

for table in c.execute("SELECT name FROM sqlite_master WHERE type='table';").fetchall():
    t = table[0]
    df = pd.read_sql('SELECT * from ' + t, conn)
    df.to_csv(t + '_one_command.csv', index = False)

See here for more info.

疧_╮線 2024-11-15 14:11:12

虽然 .mode csv.output (或 .once)命令很好,但它们只能在 SQLite 命令行界面(又名 Windows 上的 >sqlite.exe)。

如果您不使用 CLI,您仍然可以借助 sqlean-fileio 扩展,使用 fileio_append(path, string) 函数。

例如:

create table people(id integer primary key, name text);

insert into people(id, name) values
(11, 'Alice'), (12, 'Bob'), (13, 'Cindy');

select sum(
    fileio_append('people.csv', format('%d,%s', id, name) || char('10'))
  )
from people;

之后,people.csv 将具有以下内容:

11,Alice
12,Bob
13,Cindy

While .mode csv and .output (or .once) commands are fine, they only work in SQLite command-line interface (aka sqlite.exe on Windows).

If you are not using the CLI, you can still write to an output file with the help of sqlean-fileio extension, using fileio_append(path, string) function.

For example:

create table people(id integer primary key, name text);

insert into people(id, name) values
(11, 'Alice'), (12, 'Bob'), (13, 'Cindy');

select sum(
    fileio_append('people.csv', format('%d,%s', id, name) || char('10'))
  )
from people;

After that, people.csv will have the following content:

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