2012-11-22 40 views
1

CouchDB中创建文件生成以下错误,为什么在couchdb中创建文档时导致utf8字符错误导致此错误?

12> ADoc. 
[{<<"Adress">>,<<"Hjalmar Brantingsgatan 7 C">>}, 
{<<"District">>,<<"Brämaregården">>}, 
{<<"Rent">>,3964}, 
{<<"Rooms">>,2}, 
{<<"Area">>,0}] 
13> IDoc. 
[{<<"Adress">>,<<"Segeparksgatan 2A">>}, 
{<<"District">>,<<"Kirseberg">>}, 
{<<"Rent">>,9701}, 
{<<"Rooms">>,3}, 
{<<"Area">>,83}] 
14> erlang_couchdb:create_document({"127.0.0.1", 5984}, "proto_v1", IDoc). 
{json,{struct,[{<<"ok">>,true}, 
      {<<"id">>,<<"c6d96b5f923f50bfb9263638d4167b1e">>}, 
      {<<"rev">>,<<"1-0d17a3416d50129328f632fd5cfa1d90">>}]}} 
15> erlang_couchdb:create_document({"127.0.0.1", 5984}, "proto_v1", ADoc). 
** exception exit: {ucs,{bad_utf8_character_code}} 
    in function xmerl_ucs:from_utf8/1 (xmerl_ucs.erl, line 185) 
    in call from mochijson2:json_encode_string/2 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 200) 
in call from mochijson2:'-json_encode_proplist/2-fun-0-'/3 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 181) 
in call from lists:foldl/3 (lists.erl, line 1197) 
in call from mochijson2:json_encode_proplist/2 (/Users/admin/AlphaGroup/src/mochijson2.erl, line 184) 
in call from erlang_couchdb:create_document/3 (/Users/admin/AlphaGroup/src/erlang_couchdb.erl, line 256) 

以上可以在CouchDB中没有问题(IDoc的),可创建两个文件。

任何人都可以帮我弄清楚它造成的原因吗?

+0

IDoc仅使用ASCII,但ADoc具有**Brämaregården**,可能无法正确编码。 – Neil

回答

2

我认为问题在于< <“Brämaregården”>>。首先需要将unicode转换为二进制文件。示例位于以下链接中。

unicode discussion。核心功能在unicode

0

在Erlang代码中输入非ASCII字符是非常烦琐的,而不是最少的,因为它在shell中的工作方式与在编译的Erlang代码中的工作方式不同。

尝试明确地输入所述二进制为UTF-8:

<<"Br", 16#c3, 16#a4, "mareg", 16#c3, 16#a5, "rden">> 

即, “A” 是由字节C3 A4在UTF-8,和 “A” 由C3 A5表示。有很多方法可以找到这些代码;快速搜索出现了this table

通常情况下,您会从代码之外的某处获取输入,例如从文件中读取,输入到Web表单等,然后你就不会有这个问题。