MySQL匯入匯出資料的中文亂碼問題

2022-07-01 23:21:17 字數 2481 閱讀 7614

目錄

一、匯出查詢結果到檔案中

1、匯出到csv檔案

2、匯出到txt檔案

3、匯出到excel檔案

二、匯入資料到表中

1、匯入csv檔案

2、匯入txt檔案

3、匯入excel檔案

學生表

下面我們將學生表的查詢結果匯出到檔案中

select * from student into outfile 'd:/files/student.csv' fields terminated by ',' optionally enclosed by '"' escaped by '"' lines terminated by'\n';

此語句按一定格式在d:\files\下生成了乙個student.csv檔案

產生的txt檔案如下所示,這裡採用的是預設格式,按tab分隔字段。當然這裡也可以跟上面一樣,採用自定義個格式

select * from student_grade into outfile 'd:/files/student.xls';

此時,生成的excel檔案出現了亂碼問題

這是因為student表是採用utf8編碼(可以用show create table student;語句檢視一下),而excel檔案則是gb2312編碼。

所以我們採用convert將中文字段轉換成gbk編碼:

select sid, convert((sname) using gbk) as sname, convert((gender) using gbk) as gender,class, convert((major) using gbk) as major from student into outfile 'd:\files\student.xls';

這時就不會有亂碼問題了

接下來我們新建乙個student2表,並且將剛才生成的幾個檔案的資料匯入到表中

成功匯入資料:

刪除剛剛插入的所有資料:

再從txt檔案中匯入:

load data infile 'd:\\files\\student.txt' into table student2;

成功匯入資料:

刪除剛剛插入的所有資料:

再從excel檔案中匯入:

load data infile 'd:\\files\\student.xls' into table student2;

此時匯入的資料出現亂碼:

我們在匯入資料的時候指定編碼為gbk。

load data infile 'd:\\files\\student.xls' into table student2 character set gbk;    

這樣就不會出現亂碼了:

Mysql 匯入匯出csv 中文亂碼

這篇文章介紹了mysql 匯入匯出csv 中文亂碼問題的解決方法,有需要的朋友可以參考一下 匯入csv load data infile test.csv into table table name fields terminated by optionally enclosed by escape...

Mysql 匯入匯出csv 中文亂碼

這篇文章介紹了mysql 匯入匯出csv 中文亂碼問題的解決方法,有需要的朋友可以參考一下 匯入csv load data infile test.csv into table table name fields terminated by optionally enclosed by escape...

匯出匯入MySql資料

一 匯出資料庫 進入命令列,轉到mysql的安裝目錄的bin資料夾下 輸入下面的命令 1.匯出整個資料庫 mysqldump u 使用者名稱 p 資料庫名 匯出的檔名 mysqldump u root p db db.sql 然後要求你輸入密碼,輸入正確後便可以在該資料夾下找到db.sql檔案 二 ...