=ERROR REPORT==== 6-Aug-2013::16:29:09 ===
** Generic server <0.47.0> terminating
** Last message in was {<0.46.0>,
{sql_query,[6,
[73,78,83,69,82,84,32,73,78,84,79,32,116,
49,40,102,49,41,32,86,65,76,85,69,83,40,
39,39532,22825,25165,39,41,59]]},
infinity}
** When Server state == {state,#Port<0.758>,undefined,<0.46.0>,undefined,on,
false,false,off,connected,undefined,0,
[#Port<0.756>,#Port<0.757>],
#Port<0.759>,#Port<0.760>}
** Reason for termination ==
** {{badmatch,{error,einval}},
[{odbc,odbc_send,2,[{file,"odbc.erl"},{line,832}]},
{odbc,handle_msg,3,[{file,"odbc.erl"},{line,557}]},
{gen_server,handle_msg,5,[{file,"gen_server.erl"},{line,588}]},
{proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,239}]}]}
看到39532,22825,25165這三個數字了嗎?這是"馬天才「這三個字的codepoints,應該就是這個緣由。
F = <<"馬天才"/utf8>>, %% <<233,169,172,229,164,169,230,137,141>>
Sql = lists:flatten(io_lib:format("INSERT INTO t1(f1) VALUES('~s');", [F])),
odbc:sql_query(DB, Sql)
此次插入是成功啦,但插入到數據庫中是亂碼。
方法三,轉爲utf16(爲何要用utf16呢?有病亂投醫)
F = <<"馬天才"/utf16-little>>, %% <<108,154,41,89,77,98>>
Sql = lists:flatten(io_lib:format("INSERT INTO t1(f1) VALUES('~s');", [F])),
odbc:sql_query(DB, Sql)
此次插入也成功啦,但select返回時是<<108,0,154,0,41,0,89,0,77,0,98,0>>,爲何加一個0呀
數據庫中看仍然是亂碼。
3.2 用odbc:param_query(這個測試是OK的)
方法一:代碼裏直接中文
方法二:程序收到的utf8
U = <<233,169,172,229,164,169,230,137,141>>, %% 這是utf8的"馬天才「三個字
D = unicode:characters_to_binary(U, utf8, {utf16, little}), %% 轉換爲utf16-little
odbc:param_query(DB, "INSERT INTO t1(f1) VALUES(?);", [{{sql_wvarchar, 45}, [D]}])
4、查詢中文字段
odbc:sql_query(DB, "select * from t1;")
4.1 在mysql裏返回正常
返回的f1字段的值是<<108,154,41,89,77,98>>,是utf16-little的編碼,
須要傳給其它程序顯示時,須要將其轉爲utf8,方法就是:
unicode:characters_to_binary(<<108,154,41,89,77,98>>, {utf16, little}, utf8)
4.2 在oracle裏有點問題
若是定義的varchar2字段「F1」的units是byte,則會返回<<???>>,這就是爲何上面說必定要定義爲char的緣由。