在 Erlang 中使用本机 MySQL 驱动程序

发布于 2024-07-26 07:17:20 字数 4485 浏览 1 评论 0原文

我正在使用本机 MySQL 驱动程序 (http://code.google.com/p/ erlang-mysql-driver/) 和 mochiweb。 当我在 shell 模式下尝试 MySQL 驱动程序时,一切都正常。 但是,当我用 Mochiweb 编写一些代码时,它报告了以下错误:

=CRASH REPORT==== 4-Jul-2009::04:44:29 ===
  crasher:
    initial call: mochiweb_socket_server:acceptor_loop/1
    pid: <0.61.0>
    registered_name: []
    exception error: no function clause matching 
                     mysql:fetch(p1,<<"SELECT * FROM cdb_forums LIMIT 10">>)
      in function  perly_web:loop/2
      in call from mochiweb_http:headers/5
    ancestors: [perly_web,perly_sup,<0.58.0>]
    messages: []
    links: [<0.60.0>,#Port<0.965>]
    dictionary: [{mochiweb_request_body,undefined},
                  {mochiweb_request_qs,[]},
                  {mochiweb_request_post,[]},
                  {mochiweb_request_path,"/online"},
                  {mochiweb_request_cookie,
                      [{"04c_sid","hG9Oyv"},
                       {"04c_visitedfid","2"},
                       {"kQx_cookietime","2592000"},
                       {"kQx_loginuser","admin"},
                       {"kQx_activationauth",
                        "98b3mdX86fKT9dI4WyKuL61Tqxk%2BW1r6ACpHp9y8itH2xQ"},
                       {"smile","1D1"}]}]
    trap_exit: false
    status: running
    heap_size: 1597
    stack_size: 24
    reductions: 5188
  neighbours:

The code I write in Mochiweb is

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    Loop = fun (Req) ->
                   ?MODULE:loop(Req, DocRoot)
           end,
    % we’ll set our maximum to 1 million connections. (default: 2048)
    mochiweb_http:start([{max, 1000000}, {name, ?MODULE}, {loop, Loop} | Options1]),
    mysql:start_link(p1, "10.0.0.123", "root", "root", "test").

stop() ->
    mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
    "/" ++ Path = Req:get(path),
    case Req:get(method) of
        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
            case Path of
                "online" ->
                     Result1 = mysql:fetch(p1, <<"SELECT * FROM cdb_forums LIMIT 10">>),
                     Body1 = io:format("Result1: ~p~n", [Result1]),
                     Req:ok({"text/plain", Body1});

The connection Looking Good but When I add

Result1 = mysql:fetch(p1, <<"SELECT * FROM cdb_forums LIMIT 10">>),

它崩溃了。

有人能帮我吗? 先谢谢了~

//============================================== ====== 更新:

我注意到以下信息。 如果这是正确的?

=PROGRESS REPORT==== 4-Jul-2009::05:49:32 ===
          supervisor: {local,kernel_safe_sup}
             started: [{pid,<0.65.0>},
                       {name,inet_gethost_native_sup},
                       {mfa,{inet_gethost_native,start_link,[]}},
                       {restart_type,temporary},
                       {shutdown,1000},
                       {child_type,worker}]
mysql_conn: greeting version "5.1.33-log" (protocol 10) salt "ne0_m'vA" caps 63487 serverchar <<8,2,0,0,
                                                                                                0,0,0,0,
                                                                                                0,0,0,0,
                                                                                                0,0,0,0>> salt2 "!|o;vabJ*4bt"
mysql_auth send packet 1: <<5,162,0,0,64,66,15,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                            0,0,0,0,0,0,0,0,0,114,111,111,116,0,20,52,235,78,
                            173,36,251,201,242,172,139,113,231,253,181,245,3,
                            91,198,111,135>>
Link: {ok,<0.62.0>}

=SUPERVISOR REPORT==== 4-Jul-2009::05:49:32 ===
     Supervisor: {local,perly_sup}
     Context:    start_error
     Reason:     ok
     Offender:   [{pid,undefined},
                  {name,perly_web},
                  {mfa,
                      {perly_web,start,
                          [[{ip,"0.0.0.0"},
                            {port,8000},
                            {docroot,
                                "/work/mochiweb-read-only/scripts/perly/priv/www"}]]}},
                  {restart_type,permanent},
                  {shutdown,5000},
                  {child_type,worker}]

I am using native MySQL driver (http://code.google.com/p/erlang-mysql-driver/) with mochiweb. When I tried that MySQL driver in shell mode, all woked fine. But when I write some code with Mochiweb, it reported me the following error:

=CRASH REPORT==== 4-Jul-2009::04:44:29 ===
  crasher:
    initial call: mochiweb_socket_server:acceptor_loop/1
    pid: <0.61.0>
    registered_name: []
    exception error: no function clause matching 
                     mysql:fetch(p1,<<"SELECT * FROM cdb_forums LIMIT 10">>)
      in function  perly_web:loop/2
      in call from mochiweb_http:headers/5
    ancestors: [perly_web,perly_sup,<0.58.0>]
    messages: []
    links: [<0.60.0>,#Port<0.965>]
    dictionary: [{mochiweb_request_body,undefined},
                  {mochiweb_request_qs,[]},
                  {mochiweb_request_post,[]},
                  {mochiweb_request_path,"/online"},
                  {mochiweb_request_cookie,
                      [{"04c_sid","hG9Oyv"},
                       {"04c_visitedfid","2"},
                       {"kQx_cookietime","2592000"},
                       {"kQx_loginuser","admin"},
                       {"kQx_activationauth",
                        "98b3mdX86fKT9dI4WyKuL61Tqxk%2BW1r6ACpHp9y8itH2xQ"},
                       {"smile","1D1"}]}]
    trap_exit: false
    status: running
    heap_size: 1597
    stack_size: 24
    reductions: 5188
  neighbours:

The code I write in Mochiweb is

start(Options) ->
    {DocRoot, Options1} = get_option(docroot, Options),
    Loop = fun (Req) ->
                   ?MODULE:loop(Req, DocRoot)
           end,
    % we’ll set our maximum to 1 million connections. (default: 2048)
    mochiweb_http:start([{max, 1000000}, {name, ?MODULE}, {loop, Loop} | Options1]),
    mysql:start_link(p1, "10.0.0.123", "root", "root", "test").

stop() ->
    mochiweb_http:stop(?MODULE).

loop(Req, DocRoot) ->
    "/" ++ Path = Req:get(path),
    case Req:get(method) of
        Method when Method =:= 'GET'; Method =:= 'HEAD' ->
            case Path of
                "online" ->
                     Result1 = mysql:fetch(p1, <<"SELECT * FROM cdb_forums LIMIT 10">>),
                     Body1 = io:format("Result1: ~p~n", [Result1]),
                     Req:ok({"text/plain", Body1});

The connection looks good but when I added

Result1 = mysql:fetch(p1, <<"SELECT * FROM cdb_forums LIMIT 10">>),

it crashed.

Can someone help me? Thanks in advance~

//==================================================
updated:

I noticed the follwoing information. If that is correct?

=PROGRESS REPORT==== 4-Jul-2009::05:49:32 ===
          supervisor: {local,kernel_safe_sup}
             started: [{pid,<0.65.0>},
                       {name,inet_gethost_native_sup},
                       {mfa,{inet_gethost_native,start_link,[]}},
                       {restart_type,temporary},
                       {shutdown,1000},
                       {child_type,worker}]
mysql_conn: greeting version "5.1.33-log" (protocol 10) salt "ne0_m'vA" caps 63487 serverchar <<8,2,0,0,
                                                                                                0,0,0,0,
                                                                                                0,0,0,0,
                                                                                                0,0,0,0>> salt2 "!|o;vabJ*4bt"
mysql_auth send packet 1: <<5,162,0,0,64,66,15,0,8,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                            0,0,0,0,0,0,0,0,0,114,111,111,116,0,20,52,235,78,
                            173,36,251,201,242,172,139,113,231,253,181,245,3,
                            91,198,111,135>>
Link: {ok,<0.62.0>}

=SUPERVISOR REPORT==== 4-Jul-2009::05:49:32 ===
     Supervisor: {local,perly_sup}
     Context:    start_error
     Reason:     ok
     Offender:   [{pid,undefined},
                  {name,perly_web},
                  {mfa,
                      {perly_web,start,
                          [[{ip,"0.0.0.0"},
                            {port,8000},
                            {docroot,
                                "/work/mochiweb-read-only/scripts/perly/priv/www"}]]}},
                  {restart_type,permanent},
                  {shutdown,5000},
                  {child_type,worker}]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

作死小能手 2024-08-02 07:17:20

这里的实际错误是:

no function clause matching 
mysql:fetch(p1,<<"SELECT * FROM cdb_forums LIMIT 10">>)

此错误意味着该函数已存在并已导出,但不采用您传入的参数类型。 mysql 驱动程序代码表示 mysql:fetch/2 采用一个 iolist() ——您传递给它的二进制文件不是有效的 iolist()。 做:

mysql:fetch(p1,[<<"SELECT * FROM cdb_forums LIMIT 10">>])
%% or
mysql:fetch(p1,"SELECT * FROM cdb_forums LIMIT 10")

相反应该工作,因为这些都是有效的iolists。

The actual error here is:

no function clause matching 
mysql:fetch(p1,<<"SELECT * FROM cdb_forums LIMIT 10">>)

This error means the function is there and exported, but doesn't take the type of argument you're passing in. The mysql driver code says that mysql:fetch/2 takes an iolist() -- you've passed it a binary which is not a valid iolist(). Doing:

mysql:fetch(p1,[<<"SELECT * FROM cdb_forums LIMIT 10">>])
%% or
mysql:fetch(p1,"SELECT * FROM cdb_forums LIMIT 10")

instead should work as those are valid iolists.

路还长,别太狂 2024-08-02 07:17:20

我发现星号会引起问题。 明确选择一个字段会有所帮助。

I find that the asterisk causes a problem. Explicitly selecting a field helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文