当前位置:  开发笔记 > 编程语言 > 正文

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

如何解决《在Erlang中使用本机MySQL驱动程序》经验,为你挑选了1个好方法。

我使用mochiweb的本机MySQL驱动程序(http://code.google.com/p/erlang-mysql-driver/).当我在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:

我在Mochiweb写的代码是

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});

连接看起来不错,但是当我添加时

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}]

archaelus.. 5

这里的实际错误是:

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

这个错误意味着函数在那里并被导出,但是没有采用你传入的参数类型.Myql驱动程序代码说明mysql:fetch/2iolist()- 你已经传递了一个无效的二进制文件iolist().这样做:

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

相反应该工作,因为那些是有效的iolists.



1> archaelus..:

这里的实际错误是:

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

这个错误意味着函数在那里并被导出,但是没有采用你传入的参数类型.Myql驱动程序代码说明mysql:fetch/2iolist()- 你已经传递了一个无效的二进制文件iolist().这样做:

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

相反应该工作,因为那些是有效的iolists.

推荐阅读
php
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有