我正在尝试运行 sql 查询,但出现以下错误:
Incorrect string value: '\xCC_a' for column
中断 mysql 查询的 CSV 文件行:
Juan Gordon,GarcÃ_a,noman@gmail.com,,,,,,,,,,y,
SQL 错误:
<p>Error Number: 1366</p><p>Incorrect string value: '\xCC_a' for column 'last_name' at row 1</p><p>INSERT INTO `phppos_people` (`first_name`, `last_name`, `email`, `phone_number`, `address_1`, `address_2`, `city`, `state`, `zip`, `country`, `comments`) VALUES ('Juan Gordon', 'Garc�_a', 'noman@test.com', '', '', '', '', '', '', '', '')</p><p>Filename: /Library/WebServer/Documents/phppos/PHP-Point-Of-Sale/models/person.php</p><p>Line Number: 75</p> </div>
last_name 是 varchar(255) utf8_unicode_ci
$db['default']['char_set'] = 'utf8';
$db['default']['dbcollat'] = 'utf8_unicode_ci';
示例 CSV 代码
?>
last_name = GarcÌ_a
更新:我刚刚了解到,在 excel 中将文件另存为 .csv 时,编码为:Westren (Mac OS Roman),CR 作为换行符。
我认为文件编码可能会导致问题。但我需要支持它。
最佳答案
唯一导出到 Mac OS Roman 的 Excel 显然是 MS Excel for OSX。不幸的是我没有这个所以我无法检查如何使用正确的字符集导出
你现在有两个选择
a) 使用 iconv 将 CSV 转换为 UTF-8例如
iconv -f MACROMAN -t UTF8 < yourfile.csv > yourfile-utf8.csv
b) 将连接字符集设置为导入前文件的字符集
SET NAMES macroman;
在 codeigniter 中这看起来像这样
$this->db->simple_query('SET NAMES \'macroman\'');
导入完成后,不要忘记将其设置回去
$this->db->simple_query('SET NAMES \'utf8\'');
解释:
如果您的连接字符集是 UTF-8,您的数据库将排除 UTF-8 编码的数据。如果您将连接字符集设置为 macroman
并且您写入的列是 UTF-8,MySQL 会自动为您转换它。
From http://dev.mysql.com/doc/refman/5.5/en/charset-connection.html
SET NAMES 'charset_name' [COLLATE 'collation_name']
SET NAMES indicates what character set the client will use to send SQL statements to the server. Thus, SET NAMES 'cp1251' tells the server, “future incoming messages from this client are in character set cp1251.” It also specifies the character set that the server should use for sending results back to the client. (For example, it indicates what character set to use for column values if you use a SELECT statement.)
在我的 freeBSD 机器上,MySQL 编译了 macroman 字符集,我想你也会有这个。
mysql> SELECT * FROM information_schema.COLLATIONS WHERE CHARACTER_SET_NAME = 'macroman';
+---------------------+--------------------+----+------------+-------------+---------+
| COLLATION_NAME | CHARACTER_SET_NAME | ID | IS_DEFAULT | IS_COMPILED | SORTLEN |
+---------------------+--------------------+----+------------+-------------+---------+
| macroman_general_ci | macroman | 39 | Yes | Yes | 1 |
| macroman_bin | macroman | 53 | | Yes | 1 |
+---------------------+--------------------+----+------------+-------------+---------+
另见 http://dev.mysql.com/doc/refman/5.5/en/charset-charsets.html
希望对你有帮助
关于mysql - 不正确的字符串值 : '\xCC_a' for column when inserting,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14183314/