2012-11-22 39 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表單等,然後你就不會有這個問題。