1. 程式人生 > >mysql匯入檔案的經驗文章很不錯的

mysql匯入檔案的經驗文章很不錯的

本文程式碼執行環境:MySQL:5.1.26-rc-community,Windows 2003

無意中在 emule 的安裝目錄下看到了個 ip-to-country.csv 檔案。 開啟後,發現是世界各國及IP段對照的檔案。格式如下:

33996344,33996351,GB,GBR,英國
50331648,69956103,US,USA,美國
69956104,69956111,BM,BMU,百慕達群島
69956112,83886079,US,USA,美國
94585424,94585439,SE,SWE,瑞典
100663296,121195295,US,USA,美國
121195296,121195327,IT,ITA,義大利
121195328,152305663,US,USA,美國

真是個好東東!正好一個專案要用到,就準備把資料匯入到 MySQL 資料庫中。 首先在 MySQL 資料庫中建立表結構:

use testdb;

create table ip_to_country
(
ip1 int unsigned not null
,ip2 int unsigned not null
,cname1 varchar(10) not null
,cname2 varchar(10) not null
,cname3 varchar(50) not null
)
engine=innodb default charset=utf8;

我準備用 MySQL 匯入工具:mysqlimport 來完成匯入任務。 首先把資料檔案 ip-to-country.csv copy 到 d:/,為了使其和 MySQL 中表名匹配, 重新命名為 ip_to_country.csv。然後根據資料檔案格式,編寫並執行下面的 mysqlimport 指令碼:

mysqlimport --local
--user=root
--password=123456
--delete
--default-character-set="gb2312"
--fields-terminated-by=","
--fields-enclosed-by=""
--fields-escaped-by=""
--lines-terminated-by="/r/n"
testdb
"d:/ip_to_country.csv"

注意:上面的 mysqlimport 引數應寫成一行,為了便於閱讀我有意分成多行。 執行上面的 mysqlimport 命令後,發現有 Warnings:

testdb.ip_to_country: Records: 65290  Deleted: 0  Skipped: 0  Warnings: 15

資料已經匯入,不管那麼多了, 先進 MySQL 資料庫看下效果。 首先設定 character_set_results=gb2312; 然後查詢 10 條記錄出來看看,步驟如下:

mysql> set character_set_results=gb2312;

mysql> show variables like '%char%';
+--------------------------+--------------------------+
| Variable_name | Value |
+--------------------------+--------------------------+
| character_set_client | utf8 |
| character_set_connection | utf8 |
| character_set_database | utf8 |
| character_set_filesystem | binary |
| character_set_results | gb2312 |
| character_set_server | utf8 |
| character_set_system | utf8 |
| character_sets_dir | D:/MySQL/share/charsets/ |
+--------------------------+--------------------------+
mysql> select * from ip_to_country limit 10;

+-----------+-----------+--------+--------+--------+
| ip1 | ip2 | cname1 | cname2 | cname3 |
+-----------+-----------+--------+--------+--------+
| 33996344 | 33996351 | GB | GBR | ? |
| 50331648 | 69956103 | US | USA | |
| 69956104 | 69956111 | BM | BMU | |
| 69956112 | 83886079 | US | USA | |
| 94585424 | 94585439 | SE | SWE | |
| 100663296 | 121195295 | US | USA | |
| 121195296 | 121195327 | IT | ITA | |
| 121195328 | 152305663 | US | USA | |
| 152305664 | 152338431 | GB | GBR | ? |
| 152338432 | 167772159 | US | USA | |
+-----------+-----------+--------+--------+--------+

結果發現國家的中文名稱都是亂碼。奇怪,已經把 mysqlimport 的 default-character-set 引數設為:gb2312,為什麼會有亂碼? 最後不得以,只好在 MySQL 資料庫中, 把表 ip_to_country 的字符集改為 gb2312。

mysql> alter table ip_to_country default character set gb2312;
mysql> alter table ip_to_country convert to character set gb2312;

然後重新執行匯入命令 mysqlimport,這時候發現 MySQL 亂碼問題已解決, 中文國家名字可以正常顯示:

mysql> select * from ip_to_country limit 10;

+-----------+-----------+--------+--------+------------+
| ip1 | ip2 | cname1 | cname2 | cname3 |
+-----------+-----------+--------+--------+------------+
| 33996344 | 33996351 | GB | GBR | 英國 |
| 50331648 | 69956103 | US | USA | 美國 |
| 69956104 | 69956111 | BM | BMU | 百慕達群島 |
| 69956112 | 83886079 | US | USA | 美國 |
| 94585424 | 94585439 | SE | SWE | 瑞典 |
| 100663296 | 121195295 | US | USA | 美國 |
| 121195296 | 121195327 | IT | ITA | 義大利 |
| 121195328 | 152305663 | US | USA | 美國 |
| 152305664 | 152338431 | GB | GBR | 英國 |
| 152338432 | 167772159 | US | USA | 美國 |
+-----------+-----------+--------+--------+------------+

留下一個問題:mysqlimport 到底能不能把文字檔案中的 gb2312 字元 轉換成 utf8 匯入到 MySQL 資料庫中?

雖然問題看起來已經解決了,但我還想試下 MySQL load data 命令。 mysqlimport 雖然把資料匯入資料庫了,但還有 15 Warnings 在鬧心。 我本想利用 mysqlimport 自身的功能來檢視這些 Warnings 到底是怎麼回事, 但翻翻手冊,仍無計可施。MySQL 中有個 show warnings 給我一線希望。 我這樣想:先在 MySQL 中執行 Load data,然後 show warnings 不就 可以找到問題所在了嗎?

mysql> truncate table ip_to_country;

mysql>
mysql> load data infile "d:/ip_to_country.csv"
replace into table ip_to_country
character set gb2312
fields terminated by "," enclosed by ""
lines terminated by "/r/n";

ERROR 1262 (01000): Row 6737 was truncated; it contained more data than there were input columns

暈,又出現個攔路虎:ERROR 1262 (01000): Row 6737 was truncated; it contained more data than there were input columns. 最後發現問題是 sql_mode 的問題。

mysql> show variables like '%sql_mode%';

+---------------+----------------------------------------------------------------+
| Variable_name | Value |
+---------------+----------------------------------------------------------------+
| sql_mode | strict_trans_tables,no_auto_create_user,no_engine_substitution |
+---------------+----------------------------------------------------------------+

mysql> set sql_mode='no_auto_create_user,no_engine_substitution';

把 strict_trans_tables 從 sql_mode 中去掉,再次執行 MySQL Load data

mysql>  load data infile "d:/ip_to_country.csv"
replace into table ip_to_country
character set gb2312
fields terminated by "," enclosed by ""
lines terminated by "/r/n";

Query OK, 65290 rows affected, 15 warnings (0.63 sec)
Records: 65290 Deleted: 0 Skipped: 0 Warnings: 15

接下來,用 MySQL show warnings 命令,來找警告的詳細描述:

mysql> show warnings;

+---------+------+-------------------------------------------------------------------------------+
| Level | Code | Message |
+---------+------+-------------------------------------------------------------------------------+
| Warning | 1262 | Row 6737 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 6817 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 6914 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 6916 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 6918 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 6988 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 7028 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 7226 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 7569 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 7791 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 47856 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 47885 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 49331 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 49539 was truncated; it contained more data than there were input columns |
| Warning | 1262 | Row 49547 was truncated; it contained more data than there were input columns |
+---------+------+-------------------------------------------------------------------------------+

根據行號 Row 6737 到 ip_to_country.csv 中檢視,發現果然有問題,國家中文名中間多了個逗號 “,”

1089579216,1089579223,VI,VIR,維京群島,美國

看來,需要在表 ip_to_country 中再增加一列,來存放多出的內容。於是修改表結構:

mysql> alter table ip_to_country add column cname4 varchar(50) null;

再次執行 mysql load data,資料順利匯入。這時仍有警告,這些警告是因為 檔案中的大部分資料行只有 5 列,而表中有 6 列,因此 MySQL 才 Warning。

把表 ip_to_country 的字符集改為 utf8,看有沒有亂碼:

truncate table ip_to_country;
alter table ip_to_country default character set utf8;
alter table ip_to_country convert to character set utf8;

再次,執行 MySQL Load data 命令:

mysql>  load data infile "d:/ip_to_country.csv"
replace into table ip_to_country
character set gb2312
fields terminated by "," enclosed by ""
lines terminated by "/r/n";

Query OK, 65290 rows affected, 65275 warnings (0.64 sec)
Records: 65290 Deleted: 0 Skipped: 0 Warnings: 65275

懷著激動的心情,select:

mysql> select * from ip_to_country where cname4 is not null limit 10;

+------------+------------+--------+--------+----------+--------+
| ip1 | ip2 | cname1 | cname2 | cname3 | cname4 |
+------------+------------+--------+--------+----------+--------+
| 1089579216 | 1089579223 | VI | VIR | 維京群島 | 美國 |
| 1093062144 | 1093062399 | VI | VIR | 維京群島 | 美國 |
| 1097896192 | 1097897215 | VI | VIR | 維京群島 | 美國 |
| 1097947136 | 1097949183 | VI | VIR | 維京群島 | 美國 |
| 1097951232 | 1097953279 | VI | VIR | 維京群島 | 美國 |
| 1101625344 | 1101625407 | VI | VIR | 維京群島 | 美國 |
| 1101971072 | 1101971079 | VI | VIR | 維京群島 | 美國 |
| 1113864768 | 1113864783 | VI | VIR | 維京群島 | 美國 |
| 1119428608 | 1119432703 | VI | VIR | 維京群島 | 美國 |
| 1123590144 | 1123594239 | VI | VIR | 維京群島 | 美國 |
+------------+------------+--------+--------+----------+--------+

可見,MySQL load data infile 指令,可以實現不同字符集之間的轉換。

總結:

show warnings;是不錯的方法

在匯入檔案時

SET character_set_database=gbk是更不錯的方法

相關推薦

mysql匯入檔案經驗文章不錯

本文程式碼執行環境:MySQL:5.1.26-rc-community,Windows 2003 無意中在 emule 的安裝目錄下看到了個 ip-to-country.csv 檔案。 開啟後,發現是世界各國及IP段對照的檔案。格式如下: 33996344,339

使用mysql匯入檔案

一,文字文件格式的匯入(mysql)  1. 表tt的格式:  CREATE TABLE `tt` (  `ind` int NOT NULL auto_increment,  `name` char(100) default NULL,  PRIMARY KEY  (`i

mysql匯入檔案出現Data truncated for column 'xxx' at row 1的原因

mysql匯入檔案的時候很容易出現"Data truncated for column 'xxx' at row x",其中字串裡的xxx和x是指具體的列和行數. 有時候,這是因為資料型別的不對應,或者字串長度不夠而造成的. 但是,經常出現row 1就報錯,並且感覺它沒什麼

mysql匯入檔案過大報錯解決辦法

mysql 匯入sql檔案過大時 , mysql會報錯Lost connection to MySQL server during query ,在my.ini配置檔案 mysqld 節點下新增 max_allowed_packet = 900M 配置MyS

Linux時間子系統之一:clock source(時鐘源)這系列文章不錯!

origin: http://blog.csdn.net/droidphone/article/details/7975694 clock source用於為linux核心提供一個時間基線,如果你用linux的date命令獲取當前時間,核心會讀取當前的clock sourc

mysql匯入檔案超過100M的大資料,最大可以到2g

修改mysql的配置檔案my.ini max_allowed_packet = 256M 新增 wait_timeout=2880000 interactive_timeout = 2880000 如果沒有修改以上配置,效率非常低   在windows中可以通過以下方式匯入

使用資料泵匯出和匯入檔案大小差別

oracle在生產環境,使用資料泵匯出和匯入大小可能差別很大,導致新庫空間不夠。 原因:資料泵匯出後文件大小可能比實際匯入檔案小很多,主要是因為索引,其他的大小區別不大; 因為匯出只是匯出索引的定義和在哪建索引,匯入回重新執行匯出的sql語句生成索引,索引可能會很大。

】phpMyAdmin中匯入mysql資料庫檔案時出錯:您可能正在上傳大的檔案,請參考文件來尋找解決辦法

用phpMyAdmin去匯入90M左右的mysql資料庫檔案時出錯: 您可能正在上傳很大的檔案,請參考 文件 來尋找解決方法。 【解決過程】 1.很明顯,是檔案太大,無法匯入。即上傳檔案大小有限制。 所以要去解除此限制。 之前其實也遇到類似的問題,之前就解決了

mysql 查詢結果匯入檔案或excel 檔案匯入資料庫

查詢結果匯入檔案或excel 方法1 進入mysql伺服器中 select * from www limit 1000 into outfile '/xusy/www.xls'; 注意檔名外有單引號 如果方法1不行,當連線外部伺服器 方法2 直接進行操作 mysql

mysql匯入資料load data infile用法(將txt檔案中的資料匯入表中)

我們常常匯入資料!mysql有一個高效匯入方法,那就是load data infile 下面來看案例說明   基本語法: load data  [low_priority] [local] infile 'file_name txt' [replace | ignor

mysql 匯入超大檔案到資料庫

向mysql資料庫中匯入超大檔案資料庫時,如果直接用mysql命令的source + 要匯入的檔案,如果匯入的資料中含有中文,可能會出現錯誤,導致匯入中斷。 所以,如果要匯入的檔案過大,可以使用mysql -u root -p (資料庫密碼) 資料庫名稱 < 要匯入檔案的路徑+檔名 1、首先已

mysql 匯入大型檔案

本地匯入超過2G的mysql檔案,navicat已經不再適用,這裡安利一款神器,laragon。安裝後自帶nmp或者amp環境,還有composer,cmder等等一系列神器,不用再單個安裝。 如果沒有下載cmder,就用cmd也是可以的。 基本命令: mysql -uroo

Java過濾器Filter使用詳解(轉載自別人的文章,感覺不錯,適合我這樣的小白)

Java過濾器Filter使用詳解    轉載請註明原文地址:http://www.cnblogs.com/ygj0930/p/6374212.html  在我的專案中有具體應用:https://github.com/ygj0930/CoupleSpace

mysql 自動化序列號,匯入檔案

1、匯入檔案,匯入檔案用“,”隔開 LOAD DATA LOCAL INFILE 'D:\\test.txt' INTO TABLE `cmdb_develop`.`RES_ATTRIBUTE` CHARACTER SET 'gbk' FIELDS ESCAPED BY '\\' TER

mysql 匯入sql檔案,出現ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL報錯

ERROR 1418 (HY000): This function has none of DETERMINISTIC, NO SQL, or READS SQL mysql的設定預設是不允許建立函式 1、更改全域性配置       SET GLOBAL l

mysql 匯入匯出sql檔案

  一、匯出資料庫用mysqldump命令(注意mysql的安裝路徑,即此命令的路徑):1、匯出資料和表結構:mysqldump -u使用者名稱 -p密碼 資料庫名 > 資料庫名.sql#/usr/local/mysql/bin/   mysqldump -uroot -

mysql匯入和匯出檔案

匯入操作,可以把一個檔案裡的資料儲存進一張表。匯入語句格式為: LOAD DATA INFILE '檔案路徑和檔名' INTO TABLE 表名字; 匯出是把資料庫某個表中的資料儲存到一個檔案之中。匯出語句基本格式為: SELECT 列1,列2 INTO OUTFI

mysql匯入匯出sql檔案

window下 1.匯出整個資料庫 mysqldump -u 使用者名稱 -p 資料庫名 > 匯出的檔名 mysqldump -u dbuser -p dbname > dbname.sql 2.匯出一個表 mysqldump -u 使用者名稱 -p

MySQL匯入‘.frm’、‘.MYD’、‘.MYI’檔案

.frm、.myd、.myi檔案,也就是說是MySQL的原始資料檔案,這三個檔案分別是: .frm 表結構檔案 .myd 表資料檔案 .myi 表索引檔案 首先,如果你知道這個資料的表的結構,就用資料庫工具建立一樣和該資料結構一樣的表,表名應該和檔案的名稱保持一致。然後

MySQL匯入SQL檔案及常用命令

在MySQL Qurey   Brower中直接匯入*.sql指令碼,是不能一次執行多條sql命令的,在mysql中執行sql檔案的命令: mysql> source   d:/myprogram/database/db.sql; 另附mysql常用命令: 一)