0

我有多个文本文件,我想将它们导入到 SQLite 中的虚拟 FTS 表中。此处的文档提到将大约 500,000 个文件从安然电子邮件数据集中插入到单个表中。

我不明白这是怎么做到的。

Oreilly在这里说只使用 INSERT、UPDATE、DELETE,但我不清楚如何以这种方式从多个外部文件中获取内容。

使用 .insert 从命令行调用 SQLite 的外部脚本是最佳方式吗?

如果是这样,如何防止文件中的任意文本被解释为新列?

非常感谢您的帮助!

4

1 回答 1

2

插入是通过正常的 INSERT 语句完成的,如下所示:

INSERT INTO enrondata(content) VALUES('From: georgeanne.hodges@enron.com
To: energy.dl-ga-all_ubsw@enron.com
Subject: Expense Reports
Date: Tue, 5 Feb 2002 09:06:41 -0800 (PST)

In anticipation of the imminent closing of the UBSW Energy transaction, please have all outstanding employee expenses in your possession filed through Enron''s XMS system as soon as possible.
...');

您必须使用一些脚本将电子邮件转换为有效的 SQL 命令,即复制所有引号 ( '),并在文件内容周围添加 INSERT 语句。

或者,使用一些脚本读取所有数据并直接插入;例如,在 Python 中:

import sqlite3
db = sqlite3.connect("enron.db")
cursor = db.cursor()
cursor.execute("CREATE TABLE ...")
for each mail ...:
    content = ...
    cursor.execute("INSERT INTO enrondata(content) VALUES(?)", [content])
cursor.close()
db.commit()
db.close()
于 2014-07-03T07:29:23.143 回答