How to intentionally insert strings with the wrong encoding in Mysql?

心已入冬 提交于 2019-12-11 03:06:43

问题


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


回答1:


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 


来源:https://stackoverflow.com/questions/14196455/how-to-intentionally-insert-strings-with-the-wrong-encoding-in-mysql

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!