ruby - saving utf8mb4 strings to mysql -
i have json string has utf8mb4 characters. first parse json, , encode result json. after save resulting json mysql database. (this simplified flow of execution, there real-life reason why first parse json , encode back).
the problem database ends prefix of json string put there , cut off right @ first utf8mb4 character.
here code:
require 'json' require 'mysql2' tablename = 'my_table' settings = { :database => "my_database", :host => "localhost", :password => "my_password", :username => "my_username" } @database = mysql2::client.new settings @json = %q({"test":"begin \ud83d\ude04\ud83d\udc4d\ud83d\udc4f\ud83d\udd14 end"}) begin obj = json.parse @json rescue json::parsererror => e @json.force_encoding 'utf-8' encoded = @json.valid_encoding? ? @json : @json.encode!('utf-8', invalid: :replace, undef: :replace) obj = json.parse encoded end q = "create table if not exists `#{tablename}` (json text not null) engine=innodb default charset=utf8" @database.query q text = @database.escape json.generate obj q = "insert ignore `#{tablename}` (json) values('#{text}')" @database.query q q = "select * `#{tablename}`" rs = @database.query q rs.each {|r| p r }
the output is:
{"json"=>"{\"test\":\"begin "}
i have no idea why happens, appreciate help!
thanks @muistooshort helping me find way fix this:
... settings = { ... :encoding => 'utf8mb4' } ... q = "create table ... default charset=utf8mb4" ...
this works engines support utf8mb4 of course.
Comments
Post a Comment