1

我尝试使用 erlang 和 epgsql 执行简单的 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}]}

我怎么了?我该如何解决?

谢谢你。

4

3 回答 3

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 不存在或者你没有使用它的权限。

于 2012-04-27T21:10:24.877 回答
2

来自 epgsql文档

错误

来自 PostgreSQL 后端的错误返回为 {error, #error{}},请参阅 pgsql.hrl 了解记录定义。epgsql 函数也可能返回 {error, What} 其中 What 是以下之一:

{unsupported_auth_method, Method} - 所需的身份验证方法不受支持 timeout - 请求超时关闭 - 连接已关闭 sync_required - 发生错误并且必须调用 pgsql:sync

尝试包含 pgsql.hrl,捕获错误并打印错误消息,这应该指向正确的方向。

于 2012-04-27T13:33:45.917 回答
2

尝试简单地做

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

看看他从服务器返回了什么,也许你有错字或表格不存在等。

也看看这个http://www.erlangatwork.com/2009/01/erlang-and-postgresql.html

于 2012-04-27T13:13:35.940 回答