是否可以将 .gzip 文件导入到 sqlite 中/导入时可以跳过某些列吗?

发布于 2024-08-29 12:42:18 字数 120 浏览 3 评论 0原文

我尝试使用 .import 但它似乎仅限于 csv 和分隔文件。可以导入gzip文件吗?或者至少,从命令行进行管道传输?

另外,我可以跳过一些不需要的列,例如 mysql“LOAD DATA INFILE”吗?

I tried to play around with .import but it seems to limited with csv and delimited file. Is it possible to import gzip file ? or at least, pipe from command line ?

Also, could I skip some un-wanted column like mysql "LOAD DATA INFILE" ?

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

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

发布评论

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

评论(5

嗫嚅 2024-09-05 12:42:18

如果您不想使用命名管道,您还可以:

zcat $YOURFILE.gz | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"

如果您需要在导入之前修改内容,您可以使用 perl (或 awksed,无论如何)在 zcat 和 sqlite 命令之间。

例如,如果您的文件已使用竖线字符作为分隔符,并且您只想导入第 0 至 3 列和第 5 至 6 列:

zcat $YOURFILE.gz | perl -F'\|' -anle 'print join("|", @F[0..3,5..6])' | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"

If you don't want to use named pipes, you could also:

zcat $YOURFILE.gz | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"

If you need to modify stuff before import, you could use perl (or awk, sed, whatever) between the zcat and sqlite commands.

For example, if your file already uses the pipe character as a delimiter and you would like to import only columns 0 to 3 and 5 to 6:

zcat $YOURFILE.gz | perl -F'\|' -anle 'print join("|", @F[0..3,5..6])' | sqlite3 $YOURDB.sqlite ".import /dev/stdin $TABLENAME"
丿*梦醉红颜 2024-09-05 12:42:18
$ mkfifo tempfile
$ zcat my_records.csv.gz > tempfile

这就像魔术一样!

虽然mkfifo确实创建临时文件,但该文件的大小为0字节。
运行此命令时 $ zcat my_records.csv.gz > tempfile,它将在命令提示符处停止。
这允许您运行

sqlite3> .import tempfile db_table

sqlite3 完成导入命名管道后,zcat 命令也将完成运行。然后您可以删除命名管道。

$ rm -f tempfile
$ mkfifo tempfile
$ zcat my_records.csv.gz > tempfile

This works like magic!

Although the mkfifo does create temporary file, the size of this file is 0 byte.
When running this command $ zcat my_records.csv.gz > tempfile, it will halt at the command prompt.
This allows you to run

sqlite3> .import tempfile db_table

After sqlite3 finished importing the named pipe, zcat command will also finish running. You can then remove the named pipe.

$ rm -f tempfile
罗罗贝儿 2024-09-05 12:42:18
zcat data.gz |\
  cat <(echo -e ".separator ','\n.import /dev/stdin dest_table") - |\
  sqlite3 db.sqlite

工作得很好(Linux)。

zcat data.gz |\
  cat <(echo -e ".separator ','\n.import /dev/stdin dest_table") - |\
  sqlite3 db.sqlite

works nicely (linux).

浮生面具三千个 2024-09-05 12:42:18

您可以创建命名管道。它的行为就像一个普通文件,但会即时解压缩。 SQLite对此一无所知。

事实证明,维基百科上的示例是使用 gzip 的。 http://en.wikipedia.org/wiki/Named_pipe

You can create a named pipe. It will act like a normal file but decompress on the fly. SQLite will know nothing about it.

It turns out the example on wikipedia is with gzip. http://en.wikipedia.org/wiki/Named_pipe

自演自醉 2024-09-05 12:42:18

您可以为数据编写一个解析器,将其转换为一系列 SQL 语句。 Perl 是一种很好的语言。它甚至可以处理 gzip 文件

您是否在 *Nix 操作系统中运行此程序?如果是这样,您可以创建一个临时文件来保存解压缩的数据:

tf="$(mktemp)" &&
zcat <my_records.csv.gz >"$tf"
sqlite3 /path/to/database.sqlite3 ".import $tf"
rm -f "$tf"

You could write a parser for data that would convert it to a series of SQL statements. Perl is a good language for that. It can even handle gzip'd files.

Are you running this in a *Nix OS? If so, you could create a temporary file to hold the decompressed data:

tf="$(mktemp)" &&
zcat <my_records.csv.gz >"$tf"
sqlite3 /path/to/database.sqlite3 ".import $tf"
rm -f "$tf"
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文