sequel never returns utf-8, just ascii-8bit
Asked Answered
O

2

13

There is this mysql database I'm trying to connect to. DataMapper fetches everything nicely in UTF-8 but Sequel always returns strings in ASCII-8bit which produces errors with .to_json.

I have tried several things in order to get it to work.

Encoding.default_external = Encoding::UTF_8  
Encoding.default_internal = Encoding::UTF_8  
DB.run 'set names utf8'  
Sequel.mysql 'db', (...), :encoding => 'utf-8'  

I have gems: mysql (2.9.0) (tried without), mysql2 (0.3.11) and sequel (3.42.0)

The only thing that works is manually forcing the encoding on every string which is MUCH less than ideal.

Orissa answered 28/12, 2012 at 13:29 Comment(1)
did you db has ASCII-8BIT encoding?Eckard
H
21

Try Sequel.mysql2 instead of Sequel.mysql. Sequel.mysql uses the old mysql driver instead of the new mysql2 driver, and I don't believe the mysql driver supports encodings.

Hejira answered 30/12, 2012 at 2:22 Comment(3)
holy damn. I can't believe it was THIS simple. Thank you.Orissa
The documentations says nothing about this. I nearly went crazy. +100 if I couldVellum
I am using Sequel like this in the hope of using mysql2 adapter, still its not retrieving Arabic characters properly: Sequel.connect("mysql2://user:pass@localhost/the_database"), any ideas?Cordellcorder
F
2

Encoding can be passed as do :

Sequel.connect("mysql2://user:pass@localhost/the_database?encoding=utf8")
Feer answered 3/3, 2015 at 16:33 Comment(1)
Excellent, works for any connectorCurative

© 2022 - 2024 — McMap. All rights reserved.