user2609733
user2609733

Reputation: 63

How to make emysql return utf-8 encoded binaries

I'm using emysql to fetch data from mysql database.

Adding pool like this:

emysql:add_pool(my_pool, 1, MysqlUser, MysqlPassword, MysqlHost, PortInt, MysqlDatabase, utf8),

emysql_util:as_record(emysql:execute(...)) 

of prepared statement returns list or records which have binaries (strings in databases). This binaries are latin1-encoded, while in database they are utf-8 strings with cyrillic symbols.

Is there any way to fix it?

Upvotes: 0

Views: 132

Answers (1)

Khashayar
Khashayar

Reputation: 2034

This is one of the most common problems that exists, you can try to run your erlang shell with

erl +pc unicode

This forces erlang to run on utf-8

so for example you will see this

Eshell V5.10.1  (abort with ^G)  
1> [1024].
"Ѐ"
2> [1070,1085,1080,1082,1086,1076].
"Юникод"
3> [229,228,246].
"åäö"
4> <<208,174,208,189,208,184,208,186,208,190,208,180>>.
<<"Юникод"/utf8>>
5> <<229/utf8,228/utf8,246/utf8>>.
<<"åäö"/utf8>>

also I recommend to you to check this talk from 'Patrik Nyblom' on 'handling unicoed' which he gave on erlang user conference in Stockholm this year. Here

If you go through this you can find many useful stuff to handle your problem

Good luck

Upvotes: 1

Related Questions