2012-04-27 107 views
1

我試着用Erlang和epgsql執行簡單的PostgreSQL的查詢。二郎和PostgreSQL

我做的:

{ok, C} = pgsql:connect("localhost", "shk", "qwerty", [{database, "mydb"}]). 
>> {ok,<0.34.0>} 

然後:

{ok, Cols, Rows} = pgsql:squery(C, "select * from users"). 

但我得到的錯誤:

=ERROR REPORT==== 27-Apr-2012::17:58:23 === 
** State machine <0.34.0> terminating 
** Last message in was {'EXIT',<0.32.0>, 
         {{badmatch, 
          {error, 
          {error,'ð\236ð¨ð\230ð\221ð\232ð\220',<<"42P01">>, 
          <<208,190,209,130,208,189,208,190,209,136,208,181, 
           208,189,208,184,208,181,32,34,109,121,100,98,34, 
           32,208,189,208,181,32,209,129,209,131,209,137, 
           208,181,209,129,209,130,208,178,209,131,208,181, 
           209,130>>, 
          [{position,<<"15">>}]}}}, 
         [{erl_eval,expr,3}]}} 
** When State == ready 
**  Data == {state,undefined,<0.35.0>,5000, 
         [{<<"application_name">>,<<>>}, 
         {<<"client_encoding">>,<<"UTF8">>}, 
         {<<"DateStyle">>,<<"ISO, DMY">>}, 
         {<<"integer_datetimes">>,<<"on">>}, 
         {<<"IntervalStyle">>,<<"postgres">>}, 
         {<<"is_superuser">>,<<"off">>}, 
         {<<"server_encoding">>,<<"UTF8">>}, 
         {<<"server_version">>,<<"9.0.7">>}, 
         {<<"session_authorization">>,<<"shk">>}, 
         {<<"standard_conforming_strings">>,<<"off">>}, 
         {<<"TimeZone">>,<<"posixrules">>}], 
         undefined,undefined,undefined, 
         {30932,488494147}, 
         {statement,undefined,undefined,undefined}, 
         73} 
** Reason for termination = 
** {{badmatch,{error,{error,'ð\236ð¨ð\230ð\221ð\232ð\220',<<"42P01">>, 
          <<208,190,209,130,208,189,208,190,209,136,208,181, 
           208,189,208,184,208,181,32,34,109,121,100,98,34, 
           32,208,189,208,181,32,209,129,209,131,209,137, 
           208,181,209,129,209,130,208,178,209,131,208,181, 
           209,130>>, 
          [{position,<<"15">>}]}}}, 
    [{erl_eval,expr,3}]} 

什麼是錯的怎麼辦?我該如何解決它?

謝謝。

+1

是您的DB和你的表稱爲「MYDB」?不應該查詢「從mydb.mytable中選擇*」? – Isac 2012-04-27 13:16:20

+0

我的數據庫稱爲「MYDB」,表是「用戶」。 – 0xAX 2012-04-27 13:25:27

+0

使用「select * from mydb.users」我得到了相同的錯誤 – 0xAX 2012-04-27 13:27:11

回答

2

嘗試簡單地做

Response = pgsql:squery(C, "select * from mydb"), 
io:format("~p~n",[Response]). 

,看看他是從服務器回饋,也許你有錯字或表不存在等

還檢查了這一點http://www.erlangatwork.com/2009/01/erlang-and-postgresql.html

+0

在Response = pgsql:squery(C,「select * from mydb」)後,我得到了同樣的結果:{error,{error,'232d \ 220',<<"42P01">>, << 208,190,209,130​​,208,189,208,190,209,136,208,181,208,189, 208,184,208,181,32,34,109,121,100,... >>, [{position,<<"15">>}]}} – 0xAX 2012-04-27 13:20:20

2

來自epgsql docs

Errors

Errors originating from the PostgreSQL backend are returned as {error, #error{}}, see pgsql.hrl for the record definition. epgsql functions may also return {error, What} where What is one of the following:

{unsupported_auth_method, Method} - required auth method is unsupported timeout - request timed out closed - connection was closed sync_required - error occured and pgsql:sync must be called

嘗試包括pgsql.hrl,捕獲錯誤並打印錯誤消息,這應該指向正確的方向。

4

錯誤字符串似乎是在俄羅斯,如果我認識的人物。

要查看您可以使用下面的命令的響應:

io:format("~ts",[<<208,190,209,130,208,189,208,190,209,136,208,181, 
        208,189,208,184,208,181,32,34,109,121,100,98,34, 
        32,208,189,208,181,32,209,129,209,131,209,137, 
        208,181,209,129,209,130,208,178,209,131,208,181, 
        209,130>>]). 
отношение "mydb" не существует 

快速谷歌翻譯讓我覺得mydb數據庫不存在,或者您沒有權限使用它。