mysql匯入csv檔案出錯解決辦法

2021-07-10 20:21:31 字數 1554 閱讀 9070

將*.csv檔案匯入mysql可使用load data infile,

概要寫法:

load data in file

對於本地檔案,使用 load data local infile 'd:\\filename.csv' 語法

具體語法使用可參照:

如果乙個表中只有英文本元則匯入不出現問題,寫法也極其簡單:

load data local infile 『d:\\ports.csv』 into table ports;

遇到的問題一:ports.csv檔案中存在中文字元,匯入資料後變成亂碼

首先修改表及字段的編碼方式

alter table ports character set gbk2312;

alter table ports modify port_chinese varchar(50) character set gbk2312;

通過 show create table ports 檢視表及字段的編碼方式,當然也可以通過視覺化工具修改欄位的編碼格式

我匯入的命令列具體寫法是:

load data local infile 』d:\\ports.csv』 into table ports  fields terminated 『,' enclosed by 『」』 lines terminated by 『\r\n』 starting by』』;

但我修改以上兩項之後我匯入資料依舊亂碼,我的csv檔案編碼格式是gbk,

在網路上找到一篇關於此問題的文章:借鑑於文章中的寫法修改語句如下:

load data local infile 』d:\\ports.csv』 into table ports  character set gbk2312  fields terminated 『,' enclosed by 『」』 lines terminated by 『\r\n』 starting by 』』;

至此亂碼變成了正確的中文字元。

遇到的問題二:在匯入資料後檢視資料時發現csv檔案中第一行有效資料丟失,而多出三行怪異的無效資料行,而且在執行匯入命令時有39個警告

首先我使用show warnings;命令檢視提示的警告內容,發現csv檔案中的列標題匯入資料庫中出現了n多警告,而出現資料錯誤的關鍵原因在 lines terminated by 『\r\n』 這句中,因為第一行列標題並沒有以回車換行字元結束,第一行標題列在轉換過程出錯,也導致了第一行有效資料在匯入過程中出現錯誤。我將csv檔案中檔案標題的最後一列標題加上一回車後儲存資料,csv中的第一行有效資料終於匯入到資料中,但卻多一行標題行資料,通過ignore命令可解決。

最後的csv匯入命令列的寫法:

load data local infile 』d:\\ports.csv』 into table ports   character set gbk2312  fields terminated 『,' enclosed by 『」』 lines terminated by 『\r\n』 starting by 』』 ignore 1 lines;

mysql匯入 csv檔案出錯

1 報錯資訊 error 1290 hy000 the mysql server is running with the secure file priv option so it cannot execute this statement 2 報錯原因 mysql檔案的匯入和匯出路徑有預設的設定,...

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...