首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网络技术 > 网络基础 >

rails3 mysql2装配,incompatible character encodings: UTF-8 and ASCII-8BIT 解决

2012-12-18 
rails3 mysql2安装,incompatible character encodings: UTF-8 and ASCII-8BIT 解决!问题环境:os: ?windows

rails3 mysql2安装,incompatible character encodings: UTF-8 and ASCII-8BIT 解决!

问题环境:

os: ?windows XP

?

F:\>ruby -v

ruby 1.9.2p180 (2011-02-18) [i386-mingw32]

?

?

?

F:\>rails -v

Rails 3.0.5

?

?

问题描述:

1、安装不了mysql2

?? ? ?rails3 默认使用mysql adapter是mysql2.?

?? 1) gem install mysql2

?? ? ? ??ERROR: ?Error installing mysql2:

?? ? ? ? The 'mysql2' native gem requires installed build tools.

?

?? ? ? ?Please update your PATH to include build tools or download the DevKit

?? ? ? ?from 'http://rubyinstaller.org/downloads' and follow the instructions

?? ? ? ?at 'http://github.com/oneclick/rubyinstaller/wiki/Development-Kit'

?

?? 2) 安装DevKit ?按照http://wuhuizhong.iteye.com/blog/875493

?

?? 3)gem install mysql2

?? ? ? ??Temporarily enhancing PATH to include DevKit...

?? ? ? ?Building native extensions. ?This could take a while...

?? ? ? ?ERROR: ?Error installing mysql2:

?? ? ? ?ERROR: Failed to build gem native extension.

?

?? ? ? ?F:/Ruby192/bin/ruby.exe extconf.rb

?? ? ? checking for rb_thread_blocking_region()... yes

?? ? ? checking for main() in -llibmysql... no

?? ? ? ? *** extconf.rb failed ***

Could not create Makefile due to some reason, probably lack of

necessary libraries and/or headers. ?Check the mkmf.log file for more

details. ?You may need configuration options.

?

?? ?4)这样问题就同?http://www.iteye.com/problems/49494

?? ?5)那就暂且使用mysql ?adapter吧

?? ? ? gemfile 中改为:?gem 'mysql', '2.8.1'

2.incompatible character encodings: UTF-8 and ASCII-8BIT

?? ?当页面中写了中文之后,比如一个label,?<%= f.label :title, '标题' %>

?? ?按错误信息google ,baidu了 n多,都没有解决。包括:

?? ?1)?html.erb文件前加这一行:<%# encoding:utf-8 %> ,并保存成了utf-8格式(这一点是需要的)

?? ?2)?http://www.iteye.com/topic/355909 这个第一点试过了,第二点找不到类似的文件

?

?? ?后来发现 new页面可以正常显示,edit页面报这个错,同是一个_form.html.erb

?

?? ?按一篇文章中说法,测试了下:

???页面代码:

?? <td><%= blog.title.encoding %></td>

?

?? ?页面显示:ASCII-8BIT

?? ?说明:因为rails从数据库取出的中文默认编码是ASCII-8BIT,如果页面是UTF-8编码, ?concat就会出现这个错误。

?

??要解决这个问题,就需要“数据库取出的中文 转为utf-8”. 找不到可用的资料。

?

解决

?? ?感谢QQ网友 寿司 ?告诉我windows下安装mysql2的命令:

?? ?gem install mysql2 -v 0.2.6 --platform x86-mingw32

?

?? ?成功安装了mysql2, ?将gemfile中改为:gem 'mysql2', '0.2.6' ?

?? ?database.yml 中adapter 改回mysql2

?

?? ?再次测试:页面代码:<td><%= blog.title.encoding %></td>

?? ?result: ?UTF-8.

?

?? ?第2个问题也迎刃而解了。再次感谢?寿司。


?

补充:

?? 看来mysql2 adapter还是有bug, 一个表有5列:

??A: id ? B:string ?C:integer ?D:string ?E:string

??从数据库查出,B列utf-8, 显示正常, D,E ?却是ASCII-8BIT。

?

?? 我重建了数据库表,调整了列的顺序,改为:

???A: id ? B:string ? D:string ?E:string?C:integer?

??从数据库查出,B,D列utf-8, 显示正常, E ?还是ASCII-8BIT。

?

?? ?再重建一次,,改为:

???A: id ? B:string ? D:string ?E:string?C:integer?

?? 这次,从数据库查出,B,D,E列utf-8,都显示正常了

?

2011-04-17 补充:

??今天遇到了这个错误:invalid date ??

??http://hot88zh.iteye.com/blog/858300

??按以上博文中的方法:复制Mysql5 server端的libmysql.dll文件,到Ruby安装目录下的bin文件夹中,即可解决~

?

??之前使用mysql adapter时,ruby bin下的libmysql.dll 是一个早期版本,现在用mysql2 adapter,就需要用mysql 5 server下的libmysql.dll

?

?

?

但是用浏览器访问时就是卡死,不知道为什么require 'mysql'class Mysql::Result def encode(value, encoding = "utf-8") String === value ? value.force_encoding(encoding) : value end def each_utf8(&block) each_orig do |row| yield row.map {|col| encode(col) } end end alias each_orig each alias each each_utf8 def each_hash_utf8(&block) each_hash_orig do |row| row.each {|k, v| row[k] = encode(v) } yield(row) end end alias each_hash_orig each_hash alias each_hash each_hash_utf8end

这个补丁是我去年尝鲜ruby 1.9时用的,那时候是好用的。现在我早就改回ruby 1.8了,不知道现在这补丁还能不能用。要是不好用,仅作参考,不负责任。

3、libmysql.dll的问题,你可以用mklink做个symbol link,把mysql安装路径里的文件软链到ruby/bin里,这样就不存在版本过期的问题了。 4 楼 jerry 2011-06-25   require 'mysql' 
 
class Mysql::Result 
  def encode(value, encoding = "utf-8") 
    String === value ? value.force_encoding(encoding) : value 
  end 
   
  def each_utf8(&block) 
    each_orig do |row| 
      yield row.map {|col| encode(col) } 
    end 
  end 
  alias each_orig each 
  alias each each_utf8 
 
  def each_hash_utf8(&block) 
    each_hash_orig do |row| 
      row.each {|k, v| row[k] = encode(v) } 
      yield(row) 
    end 
  end 
  alias each_hash_orig each_hash 
  alias each_hash each_hash_utf8 
end


这块代码,还真把我的给弄坏了。呵呵 5 楼 seamon 2011-06-27   jerry 写道require 'mysql' 
 
class Mysql::Result 
  def encode(value, encoding = "utf-8") 
    String === value ? value.force_encoding(encoding) : value 
  end 
   
  def each_utf8(&block) 
    each_orig do |row| 
      yield row.map {|col| encode(col) } 
    end 
  end 
  alias each_orig each 
  alias each each_utf8 
 
  def each_hash_utf8(&block) 
    each_hash_orig do |row| 
      row.each {|k, v| row[k] = encode(v) } 
      yield(row) 
    end 
  end 
  alias each_hash_orig each_hash 
  alias each_hash each_hash_utf8 
end


这块代码,还真把我的给弄坏了。呵呵

那就很抱歉了,去年我用的时候确实是没问题的。这一年估计也有不小的改变。仅供参考啊,不负直接或连带责任。呵呵。

热点排行