mysql 不认的字符串_如何处理数据以避免MySQL“不正确的字符串值”错误?

bd96500e110b49cbb3cd949968f18be7.png

I am trying to use a Rake task to migrate some legacy data from MS Access to MySQL. I'm working on Windows XP, using Ruby 1.8.6.

I have the encoding for Rails set as "utf8" in database.yml.

Also, the default character set for MySQL is utf8.

99% of the data is coming in fine, but every now and then I'll get a column value that gives me a error something like this:

Mysql::Error: Incorrect string value: '\x92 Comm...' for column 'name'

at row 1:

INSERT INTO `organizations` ( [...] )

VALUES('Lawyers’ Committee', [...] )

It looks as though the thing that's giving MySQL trouble is the apostrophe immediately after the "s" in the word "Lawyers".

Here's another one...

Mysql::Error: Incorrect string value: '\x99 aoc' for column 'department'

at row 1:

INSERT INTO `addresses`

[...]

'TRInfo™ aoc'

[....]

Looks like it's choking on the "TM" after "TRInfo".

Is there any Ruby or Rails method that I can run the data through to cleanse from it any characters that MySQL will choke on?

Ideally, it would be great to replace them with more palatable characters -- replace the apostrophe with a single quote and the TM symbol with the string "(TM)".

Or, if I could somehow configure MySQL to store those characters as-is without errors that would be great too.

解决方案

It looks like your input data is not in utf-8.

I did a little investigating and the styled quote used in Lawyer's is encoded as \x92 in the Windows-1252 encoding, but would be nonsense for utf-8 (when I decoded it and encoded it into utf8, I got \xe2\x80\x99).

Thus you will need to convert the input strings from windows-1252 to utf-8 (or to unicode).

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值