加載數據的例子CSV

Original post: http://anothermysqldba.blogspot.com/2013/05/load-data-example.html
我看到最近的一個問題,所以我想我會在這裡發表我的解決方案的例子,以及有關LOAD DATA上forums.mysql.com網站。

有問題的用戶得到了很多跳過行沒有警告。 用戶也想跳過標題行,我以為設置一些領域,因為它是進口的。 由於我沒有看到相關的任何數據或模式,我只是發表了以下的工作的例子:


CREATE TABLE `example` (
`Id` int(11) NOT NULL AUTO_INCREMENT,
`Column2` varchar(14) NOT NULL,
`Column3` varchar(14) NOT NULL,
`Column4` varchar(14) NOT NULL,
`Column5` DATE NOT NULL,
PRIMARY KEY (`Id`)
) ENGINE=InnoDB

Column1 Column2 Column3 Column4 Column5
1 A Foo sdsdsd 4/13/2013
2 B Bar sdsa 4/12/2013
3 C Foo wewqe 3/12/2013
4 D Bar asdsad 2/1/2013
5 E FOObar wewqe 5/1/2013

# more /tmp/example.csv
Column1,Column2,Column3,Column4,Column5
1,A,Foo,sdsdsd,4/13/2013
2,B,Bar,sdsa,4/12/2013
3,C,Foo,wewqe,3/12/2013
4,D,Bar,asdsad,2/1/2013
5,E,FOObar,wewqe,5/1/2013

> LOAD DATA LOCAL INFILE '/tmp/example.csv'
-> INTO TABLE example
-> FIELDS TERMINATED BY ','
-> LINES TERMINATED BY '\n'
-> IGNORE 1 LINES
-> (id, Column2, Column3,Column4, @Column5)
-> set
-> Column5 = str_to_date(@Column5, '%m/%d/%Y');
Query OK, 5 rows affected (0.04 sec)
Records: 5 Deleted: 0 Skipped: 0 Warnings: 0

> select * from example;
+----+---------+---------+---------+------------+
| Id | Column2 | Column3 | Column4 | Column5 |
+----+---------+---------+---------+------------+
| 1 | A | Foo | sdsdsd | 2013-04-13 |
| 2 | B | Bar | sdsa | 2013-04-12 |
| 3 | C | Foo | wewqe | 2013-03-12 |
| 4 | D | Bar | asdsad | 2013-02-01 |
| 5 | E | FOObar | wewqe | 2013-05-01 |
+----+---------+---------+---------+------------+
5 rows in set (0.00 sec)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值