Hi Jeremy,

Thanks for the reply.

Below is the code to reproduce.

In irb, The data is loaded in this way using mysql gem.
2.3.1 :001 > require 'sequel'
 => true 
2.3.1 :002 > require 'json'
 => true 
2.3.1 :003 > 
2.3.1 :004 >   DB = Sequel.connect(
"mysql://username:password@localhost/db?socket=/var/run/mysql-default/mysqld.sock"
)
 => #<Sequel::MySQL::Database: 
"mysql://username:password@localhost/db?socket=/var/run/mysql-default/mysqld.sock">
 

2.3.1 :005 > 
2.3.1 :006 >   DB.drop_table?(:clinics)
 => [:clinics] 
2.3.1 :007 > DB.create_table(:clinics) { String :name }
 => nil 
2.3.1 :008 > class Clinic < Sequel::Model; end
 => nil 
2.3.1 :009 > 
2.3.1 :010 >   Clinic.create(name: "St. Michael’s Hopsital")
 => #<Clinic @values={:name=>"St. Michael’s Hopsital"}> 
2.3.1 :011 > 
2.3.1 :012 >   Clinic.first
 => #<Clinic @values={:name=>"St. Michael\xE2\x80\x99s Hopsital"}> 
2.3.1 :013 > Clinic.first.values.to_json
Encoding::UndefinedConversionError: "\xE2" from ASCII-8BIT to UTF-8
  from (irb):13:in `encode'
  from (irb):13:in `to_json'
  from (irb):13
  from /usr/local/rvm/rubies/ruby-2.3.1/bin/irb:11:in `<main>'
2.3.1 :014 > 
2.3.1 :015 >   Clinic.plugin(:force_encoding, 'UTF-8')
 => "UTF-8" 
2.3.1 :016 > 
2.3.1 :017 >   Clinic.first
 => #<Clinic @values={:name=>"St. Michael’s Hopsital"}> 
2.3.1 :018 > Clinic.first.values.to_json
 => "{\"name\":\"St. Michael’s Hopsital\"}" 



In irb, Using mysql2 gem, I am getting the following:
2.3.1 :001 > require 'sequel'
 => true 
2.3.1 :002 > require 'json'
 => true 
2.3.1 :003 > 
2.3.1 :004 >   DB = Sequel.connect(
"mysql2://username:password@localhost/db?socket=/var/run/mysql-default/mysqld.sock"
)
 => #<Sequel::Mysql2::Database: 
"mysql2://username:password@localhost/db?socket=/var/run/mysql-default/mysqld.sock">
 

2.3.1 :005 > 
2.3.1 :006 >   class Clinic < Sequel::Model; end
 => nil 
2.3.1 :007 > Clinic.first
 => #<Clinic @values={:name=>"St. Michael’s Hopsital"}> 
2.3.1 :008 > Clinic.first.values.to_json
 => "{\"name\":\"St. Michael’s Hopsital\"}" 
2.3.1 :009 > 
2.3.1 :010 >   Clinic.plugin(:force_encoding, 'UTF-8')
 => "UTF-8" 
2.3.1 :011 > 
2.3.1 :012 >   Clinic.first
 => #<Clinic @values={:name=>"St. Michael’s Hopsital"}> 
2.3.1 :013 > Clinic.first.values.to_json
 => "{\"name\":\"St. Michael’s Hopsital\"}" 


Here, I wanted to get "St. Michael’s Hopsital". Even if I use 
"encoding=utf8" options, I am still getting "’".

Can you please help me how I could do it?

Thanks,
Satya

On Saturday, March 24, 2018 at 4:26:24 PM UTC+5:30, Satyanarayana Gandham 
wrote:
>
> Hi,
>
> I am trying to migrate from using mysql gem to mysql2 gem because of some 
> DB transaction related issues. But, when I retrieve data from tables, I am 
> getting the data as "St. Michael’s Hopsital" instead of "St. Michael’s 
> Hopsital". When I am using mysql gem, I used force_encoding plugin with 
> 'UTF-8'. In DB, it is stored as "St. Michael’s Hopsital".
>
> Can you guys please help me solving this issue?
>
> Thanks,
> Satya
>

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to