如何将 CSV 文件导入 MySQL 表? [英] How do I import CSV file into a MySQL table?

查看:45
本文介绍了如何将 CSV 文件导入 MySQL 表?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个来自客户端的非规范化事件日志 CSV,我试图将其加载到 MySQL 表中,以便我可以重构为合理的格式.我创建了一个名为CSVImport"的表,该表为 CSV 文件的每一列都有一个字段.CSV 包含 99 列,因此这本身就是一项艰巨的任务:

CREATE TABLE 'CSVImport' (id INT);ALTER TABLE CSVImport ADD COLUMN Title VARCHAR(256);ALTER TABLE CSVImport ADD COLUMN Company VARCHAR(256);ALTER TABLE CSVImport ADD COLUMN NumTickets VARCHAR(256);...ALTER TABLE CSVImport Date49 ADD COLUMN Date49 VARCHAR(256);ALTER TABLE CSVImport Date50 ADD COLUMN Date50 VARCHAR(256);

表上没有约束,所有字段都包含 VARCHAR(256) 值,除了包含计数(用 INT 表示)、是/否(用 BIT 表示)、价格(用 DECIMAL 表示)和文字简介(由 TEXT 表示).

我尝试将数据加载到文件中:

LOAD DATA INFILE '/home/paul/clientdata.csv' INTO TABLE CSVImport;查询 OK,2023 行受影响,65535 条警告(0.08 秒)记录:2023 删除:0 跳过:0 警告:198256选择 * 从 CSVImport;|空 |空 |空 |空 |空 |...

整个表被NULL填满.

我认为问题在于文本简介包含不止一行,并且 MySQL 正在解析文件,好像每个新行都对应于一个数据库行.我可以毫无问题地将文件加载到 OpenOffice 中.

clientdata.csv 文件包含 2593 行和 570 条记录.第一行包含列名.我认为它是逗号分隔的,文本显然是用双引号分隔的.

更新:

如有疑问,请阅读手册:

I have an unnormalized events-diary CSV from a client that I'm trying to load into a MySQL table so that I can refactor into a sane format. I created a table called 'CSVImport' that has one field for every column of the CSV file. The CSV contains 99 columns , so this was a hard enough task in itself:

CREATE TABLE 'CSVImport' (id INT);
ALTER TABLE CSVImport ADD COLUMN Title VARCHAR(256);
ALTER TABLE CSVImport ADD COLUMN Company VARCHAR(256);
ALTER TABLE CSVImport ADD COLUMN NumTickets VARCHAR(256);
...
ALTER TABLE CSVImport Date49 ADD COLUMN Date49 VARCHAR(256);
ALTER TABLE CSVImport Date50 ADD COLUMN Date50 VARCHAR(256);

No constraints are on the table, and all the fields hold VARCHAR(256) values, except the columns which contain counts (represented by INT), yes/no (represented by BIT), prices (represented by DECIMAL), and text blurbs (represented by TEXT).

I tried to load data into the file:

LOAD DATA INFILE '/home/paul/clientdata.csv' INTO TABLE CSVImport;
Query OK, 2023 rows affected, 65535 warnings (0.08 sec)
Records: 2023  Deleted: 0  Skipped: 0  Warnings: 198256
SELECT * FROM CSVImport;
| NULL             | NULL        | NULL           | NULL | NULL               | 
...

The whole table is filled with NULL.

I think the problem is that the text blurbs contain more than one line, and MySQL is parsing the file as if each new line would correspond to one databazse row. I can load the file into OpenOffice without a problem.

The clientdata.csv file contains 2593 lines, and 570 records. The first line contains column names. I think it is comma delimited, and text is apparently delimited with doublequote.

UPDATE:

When in doubt, read the manual: http://dev.mysql.com/doc/refman/5.0/en/load-data.html

I added some information to the LOAD DATA statement that OpenOffice was smart enough to infer, and now it loads the correct number of records:

LOAD DATA INFILE "/home/paul/clientdata.csv"
INTO TABLE CSVImport
COLUMNS TERMINATED BY ','
OPTIONALLY ENCLOSED BY '"'
ESCAPED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 LINES;

But still there are lots of completely NULL records, and none of the data that got loaded seems to be in the right place.

解决方案

The core of your problem seems to be matching the columns in the CSV file to those in the table.

Many graphical mySQL clients have very nice import dialogs for this kind of thing.

My favourite for the job is Windows based HeidiSQL. It gives you a graphical interface to build the LOAD DATA command; you can re-use it programmatically later.

Screenshot: "Import textfile" dialog

To open the Import textfile" dialog, go to Tools > Import CSV file:

这篇关于如何将 CSV 文件导入 MySQL 表?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆