如何准备文件中的数据并将其插入到 Mysql DB 中?

发布于 2024-11-08 06:17:00 字数 1032 浏览 5 评论 0原文

我将这些数据保存在 XLS 文件中。 XLS 文件混乱不堪,其中包含大量我不需要的数据。我将清理文件并重新排列它。

我有这样的内容:

Level  - Code  - Description
 1        A       'foo foo'
 2       12331    'bar bar'
 3       13123    'bla bla'
 4       21321    'plim bar'
 5       12111    'foo plim'
 5       12111    'plim bla'
 5       12111    'bla plim'
 1        B       'bla bar'
 n        ...      ...

Level 定义层次结构位置,例如 1 是顶层层次结构。 5 是最低的。

我将利用邻接列表模型来存储此信息。 所以我相信我必须像这样存储它:

id - description - parent_id
 1    'foo foo'      NULL
 2    'bar bar'       1
 3    'bla bla'       2
 4    'plim bar'      3
 5    'foo plim'      4
 6    'plim bla'      4
 7    'bla plim'      4
 8    'bla bar'      NULL
 n     ...            ...

像这样插入此信息的最佳(最快、最简单)方法是什么? 我应该转换为 CSV 吗?我应该如何格式化 XLS 文件,以便可以通过维护层次结构来插入此信息?

XLS 文件中有 9000 行,我很想避免逐一这样做!

我们应该考虑哪些方法才能成功导入此数据?

更新: 我有这个 mysqlworkbench 软件...我使用的是 Ubuntu,这里都是开源的。

预先非常感谢。

I have this data on an XLS file.
The XLS file is a mess with a lot of data that I don't need. I will clean the file and I can re-arrange it.

I have something like this:

Level  - Code  - Description
 1        A       'foo foo'
 2       12331    'bar bar'
 3       13123    'bla bla'
 4       21321    'plim bar'
 5       12111    'foo plim'
 5       12111    'plim bla'
 5       12111    'bla plim'
 1        B       'bla bar'
 n        ...      ...

The Level defines the hierarchy position, like 1 is a top hierarchy. 5 is the lowest.

I will make use of the Adjacency List Model to store this information.
So I believe I will have to store it like this:

id - description - parent_id
 1    'foo foo'      NULL
 2    'bar bar'       1
 3    'bla bla'       2
 4    'plim bar'      3
 5    'foo plim'      4
 6    'plim bla'      4
 7    'bla plim'      4
 8    'bla bar'      NULL
 n     ...            ...

What is the best (fastest, easiest) method to insert this information like this?
Should I convert to a CSV? How should I format the XLS file so that I can insert this information by maintaining the hierarchy ?

There are 9000 lines in the XLS file, and I would love to avoid doing this one by one!

What methods should we consider in order to successfully import this data ?

Update:
I have this mysqlworkbench software... I'm on Ubuntu and all opensource here.

Thanks a lot in advance.

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

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

发布评论

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

评论(2

雨夜星沙 2024-11-15 06:17:00

如果可能的话,请使用 LOAD 命令以及类似 csv 的数据。

下面是一个示例:

LOAD DATA INFILE 'path-to-your-data-file'
INTO TABLE <your-table>
CHARACTER SET latin1
FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(col1, col2,..., coln)
;

其中 IGNORE 1 LINES 表示数据文件中的列标题将被忽略,(col1, col2,..., coln) 是数据库列使用。

您还有很多其他选项可供使用(请参阅 http:// dev.mysql.com/doc/refman/5.1/en/load-data.html) 和 LOAD 据称比 INSERT 快 20 倍大数据负载(根据http://dev.mysql.com/doc/refman/5.1/en /insert-speed.html)。

Use the LOAD command, with csv-like data, if that's at all possible.

Here's an example:

LOAD DATA INFILE 'path-to-your-data-file'
INTO TABLE <your-table>
CHARACTER SET latin1
FIELDS TERMINATED BY '\t' OPTIONALLY ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES
(col1, col2,..., coln)
;

where IGNORE 1 LINES means your column headings in the data file will be ignored, and (col1, col2,..., coln) are the database columns to use.

You have lots of other options available to you (see http://dev.mysql.com/doc/refman/5.1/en/load-data.html) and LOAD is supposedly up to 20 times faster then INSERT for large data loads (according to http://dev.mysql.com/doc/refman/5.1/en/insert-speed.html).

提赋 2024-11-15 06:17:00

您可以使用 Navicat 进行导入,免费版本即可很好。

You can use Navicat to do the importing, the free version will do just fine.

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