我正在尝试在我的数据库中填充一个表,其中一些行包含一些拉丁字符,特别是“Ñ”。此列表来自excel,我将其转换为csv文件。
我的CSV文件包含以下内容:
"1619","137601000","CITY OF LAS PIÑAS","METRO MANILA","NCR","National Capital Region","MM" "1620","137602000","CITY OF MAKATI","METRO MANILA","NCR","National Capital Region","MM" "1627","137604000","CITY OF PARAÑAQUE","METRO MANILA","NCR","National Capital Region","MM"
我使用以下命令将此列表加载到目标表:
$numrows = mysqli_num_rows($q);
if($numrows > 0) {
// ...
}
我也尝试将CHARACTER SET添加到我的load语句中,但是我得到了相同的结果:
LOAD DATA LOCAL INFILE '/my/path/to/file.csv' INTO TABLE mytable FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
但是在加载后,当我检查了我的表的内容时,包含“Ñ”的字段被截断。
| 1619 | 137601000 | CITY OF LAS PI | METRO MANILA | NCR | National Capital Region | MM | | 1620 | 137602000 | CITY OF MAKATI | METRO MANILA | NCR | National Capital Region | MM | | 1627 | 137604000 | CITY OF PARA | METRO MANILA | NCR | National Capital Region | MM |
在this resource上尝试了一些建议,但仍无济于事。
我试图提取表的内容,因为它可能只是一个表示问题,但带有“Ñ”的字段确实被截断了。
以下是有助于解决此问题的有用信息。
我用这个命令创建了我的数据库:
LOAD DATA LOCAL INFILE '/my/path/to/file.csv' INTO TABLE mytable CHARACTER SET utf8 FIELDS TERMINATED BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\r\n' IGNORE 1 LINES;
和我的表格:
CREATE DATABASE myDB DEFAULT CHARACTER SET utf8 DEFAULT COLLATE utf8_unicode_ci
操作系统变体和版本
CREATE TABLE `location` (
`id` int(4) NOT NULL AUTO_INCREMENT,
`code` int(11) NOT NULL,
`city` varchar(60) NOT NULL,
`province` varchar(30) NOT NULL,
`region` varchar(4) NOT NULL,
`regionname` varchar(40) NOT NULL,
`area` varchar(3) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
MySQL变体和版本
表格列字符设置
+-------------+--------------------+-----------------+ | column_name | character_set_name | collation_name | +-------------+--------------------+-----------------+ | id | NULL | NULL | | code | NULL | NULL | | city | utf8 | utf8_unicode_ci | | province | utf8 | utf8_unicode_ci | | region | utf8 | utf8_unicode_ci | | regionname | utf8 | utf8_unicode_ci | | area | utf8 | utf8_unicode_ci | +-------------+--------------------+-----------------+
我的数据库设置
+-------------+----------------------------------------------------------------------------------------------+ | Database | Create Database | +-------------+----------------------------------------------------------------------------------------------+ | myDbName | CREATE DATABASE `myDbName` /*!40100 DEFAULT CHARACTER SET utf8 COLLATE utf8_unicode_ci */ | +-------------+----------------------------------------------------------------------------------------------+
答案 0 :(得分:0)
截断通常由
引起SET NAMES utf8
在INSERT
(或您的情况下为LOAD DATA
)期间生效也就是说,csv文件实际上不是utf8格式。请尝试CHARACTER SET
中的其他LOAD DATA
,可能是latin1
。