如何有意地在Mysql中插入带有错误编码的字符串? [英] How to intentionally insert strings with the wrong encoding in Mysql?

查看:175
本文介绍了如何有意地在Mysql中插入带有错误编码的字符串?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是最奇怪的问题之一,我不得不问在Stackoverflow:)



我有一个遗留的,未经测试的PHP应用程序,我不能触摸。此应用程序使用Mysql和每个帐户一个数据库。所以我们有成千上万的数据库。



由于在我开始工作之前有一个错误,这个应用程序连接与错误的编码在Mysql。所以在数据库中,我们应该有é,我们实际上得到é。虽然在应用程序(由于错误的连接编码),我们得到é。



我有一个Rails应用程序管理帐户/数据库的创建。问题是Rails应用程序使用正确的编码,并且当它创建数据库时,它将插入一些PHP应用程序无法正确读取的数据。



我的问题:考虑到我有一个字符串é在Ruby,如何我有意改变为é?我可以使用任何string.encode?

注意1:我不能转储所有的数据库和修复这(这将是理想的解决方案)



注意2:我不能切换Rails连接编码,因为它使用具有正确编码的另一个数据库。

解决方案

您可以尝试使用默认字符集

  config.action_controller.default_charset ='ISO-8859- config / application.rb中的1'



原始的PHP编码看起来像ISO 8859-1或Windows-1252



我在irb上试过,发现这是工作

  1.9.3p194:002> putsé.force_encoding(ISO-8859-1)。encode(UTF-8)
é
=> nil


This is probably one of the weirdest questions that i had to ask in Stackoverflow :)

I have a legacy, non-tested PHP application which i can't touch at all. This application uses Mysql and one database per account. So we have thousands of databases.

Due to an error that was there way before i started working on it, this app connects with the wrong encoding in Mysql. So in database, where we were supposed to have "é" we actually get "é". Although in application ( due to the wrong connection encoding ), we get é.

I have a Rails application that manage the creation of accounts/databases. The problem is that the Rails application is using the correct encoding and when it creates the database, it will insert some data which the PHP application can't read properly.

My question: considering that i have a string "é" in Ruby, how to i intentionally change it to "é"? Any string.encode that i can use?

Note 1: I can't dump all databases and fix this ( which would be the ideal solution )

Note 2: I can't switch Rails connection encoding as it uses another database which has the correct encoding

解决方案

You can try to have the default charset as

config.action_controller.default_charset = 'ISO-8859-1'

in config/application.rb

The original PHP encoding looks like to be ISO 8859-1 or Windows-1252

I tried this on a irb and found that this is working

1.9.3p194 :002 > puts "é".force_encoding("ISO-8859-1").encode("UTF-8")
é
 => nil 

这篇关于如何有意地在Mysql中插入带有错误编码的字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆