解析csv資料匯入mysql的方法

2022-09-29 22:27:22 字數 996 閱讀 5185

mysql自己有個csv引擎,可以通過這個引擎來實現將csv中的btlfydbg資料匯入到mysql資料庫中,並且速度比通過php或是python寫的批處理程式快的多。

具體的實現**示例:

複製** **如下:

load data infile '/tmp/file.csv' into table _tabl程式設計客棧ename (set character utf8)

fields terminated by ','

enclosed by '程式設計客棧"'

lines terminated by '\r\n';

這段**中涉及的一些關鍵字的解釋如下:

fields terminated by '':這是指出csv檔案中欄位終止符,也就是資料之間的分隔符;

enclosed by '':指出封套符;

lines terminated by '':指行終止符

在csv文件(rfc4180)中詳細介紹了csv的格式,其中的要點有:

(1)字段之間以「,」(逗號)間隔,資料行之間使www.cppcns.com用\r\n分隔;

(2)字串以半形雙引號包圍,字串本身的雙引號用兩個雙引號表示。

通過以上的解釋,詳細對於資料匯入**應該有更好的理解了。

同樣的,csv資料能夠匯入mysql資料庫中,mysql中的資料表也能匯出csv檔案,匯出的**示例:

複製** **如下:

select * from tablename into outfile '/tmp/data.txt'

fields terminated by ','

optionally enclosed by '"'

lines terminated by '\n';

當將資料庫中的資料匯出到檔案後,要再將資料匯入到資料庫中,必須遵守匯出時的檔案中定義的格式。

本文標題: 解析csv資料匯入mysql的方法

本文位址: /wangluo/php/97536.html

解析csv資料匯入mysql的方法

mysql自己有個csv引擎,可以通過這個引擎來實現將csv中的資料匯入到mysql資料庫中,並且速度比通過php或是python寫的批處理程式快的多。具體的實現 示例 如下 load data infile tmp file.csv into table tablename set charact...

Mysql 匯入csv檔案

mysql load data infile命令可以把csv平面檔案中的資料匯入到資料庫中。linux下 load data infile home test dump ip location.csv into table ip location character set utf8 fields ...

CSV檔案匯入MySQL

1 首先看一下我本次匯入的資料,比較簡單 1 在資料庫中首先建立了乙個名為 test 的資料庫,在test資料庫下建立了乙個名為 student 的 屬性如下 column name datatype note idint 11 primary key,not null name varchar 4...