2012-04-27 104 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数据库不存在,或者您没有权限使用它。