mysql匯入資料方法(txt,csv)

來源:互聯網
上載者:User

 

mysqlimport
樣本
mysqlimport -uroot -p123456 test /tmp/mytbl.txt;
約定:檔案名稱的最後一部分為表名,以上語句匯入到表mytbl
mysqlimport必須指定資料庫,以上語句資料庫為test
 

 匯入csv

mysqlimport -uroot -p --local --lines-terminated-by="rn" --fields-terminated-by="," --fields-enclosed-by=""" test /tmp/mytbl.csv
 

load data
樣本
mysql> load data  infile '/tmp/mytbl.txt' into table mytbl
load data可以不指定資料庫
以上語句中,mysql必須有/tmp/的讀許可權
匯入csv
mysql> load data  infile '/tmp/mytbl.csv' into table mytbl fields terminated by ',' enclosed by '"' lines terminated by 'rn'
 

處理重複主鍵
替換已有值
mysql> load data infile '/tmp/mytbl.txt' replace into table mytbl fields terminated by 't' lines terminated by 'n'
表中已有則不匯入
mysql> load data infile '/tmp/mytbl.csv' ignore into table mytbl fields terminated by ',' enclosed by '"' lines terminated by 'rn'
跳過檔案行
以下樣本為跳過第一行
mysql> load data infile '/tmp/mytbl.txt' into table mytbl ignore 1 lines;
預先處理
檔案data.txt內容:
Date Time Name Weight State
2006-09-01 12:00:00 Bill Wills 200 Nevada
2006-09-02 09:00:00 Jeff Deft 150 Oklahoma
2006-09-04 03:00:00 Bob Hobbs 225 Utah
2006-09-07 08:00:00 Hank Banks 175 Texas
檔案必須被載入入如下的表
create table tbl
(
dt datetime,
last_name char(10),
first_name char(10),
weight_kg float,
st_abbrev char(2)
)
create table states
(
name varchar(20),
shortname char(2)
)
states 表中內容:
name shortname
Nevada NV
Oklahoma OK
Utah UT
Texas TX
insert into states
values('Nevada','NV')
,('Oklahoma','OK')
,('Utah', 'UT')
,('Texas', 'TX')
匯入:
load data infile '/tmp/data.txt' into table tbl
ignore 1 lines
(@date,@time,@name,@weight_lb,@state)
set dt=concat(@date,' ',@time),
     first_name=substring_index(@name,' ',1),
     last_name=substring_index(@name,' ',-1),
     weight_kg=@weight_lb * .454,
     st_abbrev = (select shortname from states where name=@state);
 
結果:
mysql> select * from tbl;                                                                                                                                               +---------------------+-----------+------------+-----------+-----------+
| dt                  | last_name | first_name | weight_kg | st_abbrev |
+---------------------+-----------+------------+-----------+-----------+
| 2006-09-01 12:00:00 | Wills     | Bill       |      90.8 | NV        |
| 2006-09-02 09:00:00 | Deft      | Jeff       |      68.1 | OK        |
| 2006-09-04 03:00:00 | Hobbs     | Bob        |    102.15 | UT        |
| 2006-09-07 08:00:00 | Banks     | Hank       |     79.45 | TX        |
+---------------------+-----------+------------+-----------+-----------+
將windows本地檔案匯入到linux下的mysql資料庫,加local
load data local infile 'C:/mytbl.txt' into table mytbl fields terminated by 't' lines terminated by 'rn'

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.