Erlang emysql iPhone表情符号编码问题 [英] Erlang emysql iPhone Emoji Encoding Issue

查看:193
本文介绍了Erlang emysql iPhone表情符号编码问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Erlang从一个MySQL数据库的iPhone客户端应用程序中存储文本(带有表情符号)。 (进入varchar列)

I'm trying to store text (with emoji) From an iPhone Client App on a MySQL database with Erlang. (Into a varchar column)

我曾经用一个使用C ++和mysqlpp的套接字连接服务器来完成它,这是非常好的。 (这是完全相同的数据库,所以我可以假设问题不是来自数据库)

I used to do it with a socket connection server done with C++ and mysqlpp, it was working great. (It is the exact same database, So I can assume that the issue is not coming from the database)

但是,我决定通过所有的Erlang可扩展性的原因,因为,我无法存储和检索正确emojis。

However, I decided to pass everything on Erlang for scalability reasons, and since, I am unable to store and retrieve correctly emojis.

我正在使用emysql与我的数据库进行通信。

I'm using emysql to communicate with my database.

当我存储时,我将此列表发送到数据库:

When I'm storing, I'm sending this list to the database :

[240,159,152,130]

当我检索时,我在这里获得:

When I'm retrieving, here what I get :

<<195,176,194,159,194,152,194,130>>

显然有一些相似之处,两条线上可以看到159,152和130,但没有240我不知道195,176和194哪里来了。

There is some similarities obviously, we can see 159, 152 and 130 on both lines, but no 240. I do not know where 195, 176 and 194 come from.

我在创建连接池时要改变emysql编码。

I though about changing the emysql encoding when creating the connection pool.

 emysql:add_pool(my_db, 3, "login", "password", "db.mydomain.com", 3306, "MyTable", utf8)

但是我似乎可以找到适合utf32编码的原子。 (有趣的是,我没有在C ++和mysqlpp上设置任何编码,它的开箱即用)。

But I can seems to find the proper atom for utf32 encoding. (The interesting thing is that I have not set any encoding on C++ and mysqlpp, it worked out of the box).

我做了一些测试... <从C ++存储,从C ++中检索(工作正常)
从Erlang存储,从Erlang检索(不工作)$

I have made some test...

storing from C++, retrieving from C++ (Works fine)
storing from Erlang, retrieving from Erlang (Does not work)
storing from Erlang, retrieving from C++ (Does not work)
storing from C++, retrieving from Erlang (Does not work)

还有一个信息,我正在Erlang使用准备的语句,而我不在C ++

One more information, I'm using prepared statement on Erlang, while I'm not on C++

任何帮助将不胜感激

根据要求,此处存储数据的查询:

AS requested, here the query for storing data :

UPDATE Table SET c=? WHERE id=?

非常简单真的...

推荐答案

这是关于utf-8编码的一切。在Erlang中,一个字符列表,在您的情况下 [240,159,152,130] ,通常不会被编码,而是unicode代码点。当您检索到数据时,您会得到一个包含utf-8编码字符字节的二进制文件。确切地说,这个编码发生在哪里,我不知道。从erlang shell:

It is all about utf-8 encoding. In Erlang a list of characters, in your case [240,159,152,130], aren't normally encoded but are the unicode code points. When you retrieved the data you got a binary containing with utf-8 encoding bytes of your characters. Exactly where this encoding occurred I don't know. From the erlang shell:

10> Bin = <<195,176,194,159,194,152,194,130>>.   
<<195,176,194,159,194,152,194,130>>
11> <<M/utf8,N/utf8,O/utf8,P/utf8,R/binary>> = Bin.
<<195,176,194,159,194,152,194,130>>
12> [M,N,O,P].
[240,159,152,130]

在erlang中处理unicode非常简单,列表中的字符通常是unicode代码点,并且非常少编码,而将它们存储在二进制文件中意味着您必须以某种方式对它们进行编码,因为二进制文件只是字节数组。默认编码为utf-8。在 unicode 模块中,有用于在unicode列表和二进制文件之间进行转换的功能。

Handling unicode in erlang is pretty simple, characters in lists are usually the unicode code points and are very rarely encoded, while storing them in binaries means you have to encode them in some way, as binaries are just arrays of bytes. The default encoding is utf-8. In the module unicode there are functions for converting between unicode lists and binaries.

这篇关于Erlang emysql iPhone表情符号编码问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆