将文本文件中的内容加载到 sqlite 表?

将文本文件中的内容加载到 sqlite 表?

问题描述:

我有只包含普通文本的简单文本文件.

I have simple text files that contain just normal texts.

我想知道是否有办法将文本内容加载到 sqlite 表格中.

I was wondering if there is a way to load the text contents to table in sqlite.

所以也许我可以创建表 myTable(nameOfText TEXT, contents TEXT);

So maybe I could create table myTable(nameOfText TEXT, contents TEXT);

然后将文本名称放入第一列,将内容放入第二列.

And then put the name of the text into the first column and contents to the second column.

如果输入文件名很困难,将内容加载到一个列表中也可以.

If putting in the name of the file is hard, loading the content into one column table is just as fine.

任何建议将不胜感激.

谢谢!!

termsql 是一种工具,可以将文件中的文本或程序 (stdin) 的输出即时转换为 sqlite 数据库.

termsql is a tool that can convert text from a file or the output of a program (stdin) on-the-fly into a sqlite database.

termsql -c nameOfText,contents -i input.txt -o myDB.db 

这将创建一个包含 nameOfText 和 content 列的表格.对于 input.txt 中的每一行一行将插入到 myDB.db 中.

This will create a table with the columns nameOfText and contents. For each line in input.txt one row will be inserted into myDB.db.

您没有告诉我们有关分隔符 nameOfText 和上下文的分隔符.由默认 termsql 假定空格是分隔符.但它应该是','例如,那么你会做这样的事情:

You didn't tell us about the delimiter nameOfText and the context are separated by. By default termsql assumes whitespace is the delimiter. But should it be ',' for example, then you would do something like this:

termsql -d ',' -c nameOfText,contents -i input.txt -o myDB.db

您可以在此处获取 termsql:https://github.com/tobimensch/termsql

You can get termsql here: https://github.com/tobimensch/termsql

Termsql 也有其他用例.您可以在一个命令中对新数据库执行 SQL 语句.以下示例将创建您的数据库,并在命令行上为内容列包含字符串test"的所有行返回 nameOfText 列.

Termsql has other usecases, too. You can do SQL statements on the new database all in one command. The following example would create your database and return the nameOfText column on the command line for all rows where the contents column contains the string 'test'.

termsql -d ',' -c nameOfText,contents -i input.txt -o myDB.db "select nameOfText from tbl where contents like '%test'"