mysql 匯入資料

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

 

 匯入csv

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

 

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

 

處理重複主鍵替換已有值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 '\r\n' 跳過檔案行以下樣本為跳過第一行mysql> load data infile '/tmp/mytbl.txt' into table mytbl ignore 1 lines; 預先處理檔案data.txt內容:DateTimeNameWeightState2006-09-0112:00:00Bill Wills200Nevada2006-09-0209:00:00Jeff Deft150Oklahoma2006-09-0403:00:00Bob Hobbs225Utah2006-09-0708:00:00Hank Banks175Texas檔案必須被載入入如下的表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 shortnameNevada NVOklahoma OKUtah UTTexas TXinsert into statesvalues('Nevada','NV'),('Oklahoma','OK'),('Utah', 'UT'),('Texas', 'TX')匯入:load data infile '/tmp/data.txt' into table tblignore 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 '\r\n' 

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.