mysqlerlangyawserlangweb

How to connect to mysql database with Erlang, Yaws


I am new to Erlang and Yaws... I am trying to connect to a mysql database but I am not sure about a few things..

Questions.
1. Do I need additional library to connect to mysql database with Erlang?
2. How Do I list available database drivers with Erlang?

I tried this code to connect.

application:start(odbc), 
ConnString = 
    "Driver={MySQL ODBC 5.1 Driver};" ++ 
    "Server=localhost;Database=db;" ++ 
    "User=root;Password=2eklmss;" ++ 
    "Option=3;", 
{ok, Conn} = odbc:connect(ConnString, []), 
Results = odbc:sql_query(Conn, "SELECT * FROM db"), 
{html,"Something here."}. 

{I am not sure about this line

"Driver={MySQL ODBC 5.1 Driver};" ++ 

that is why I asked the 2th question }

and failed with following error:

ERROR erlang code threw an uncaught exception:
 File: /Users/username/Terminal/WebServerA/var/yaws/www/index.yaws:3
Class: error
Exception: undef
Req: {http_request,'GET',{abs_path,"/"},{1,1}}
Stack: [{odbc,connect,
              ["Driver={MySQL ODBC 5.1 Driver};Server=localhost;Database=db;User=root;Password= 2eklmss;Option=3;",
               []],
              []},
        {m22,out,1,
             [{file,"/Users/username/.yaws/yaws/default/m22.erl"},
              {line,19}]},
        {yaws_server,deliver_dyn_part,8,
                     [{file,"yaws_server.erl"},{line,2851}]},
        {yaws_server,aloop,4,[{file,"yaws_server.erl"},{line,1255}]},
        {yaws_server,acceptor0,2,[{file,"yaws_server.erl"},{line,1078}]},
        {proc_lib,init_p_do_apply,3,[{file,"proc_lib.erl"},{line,240}]}]

I deleted all codes and only left this line.

out(A) -> 
    application:start(odbc).

The following error was occurred :

yaws code at /Users/username/Terminal/WebServerA/var/yaws/www/index.yaws:3 crashed or ret bad val:{error,
                                                                                                       {"no such file or directory",
                                                                                                        "odbc.app"}} 
Req: {http_request,'GET',{abs_path,"/"},{1,1}}

Solution

  • The error says that it can't find odbc application in its path. So it seems that there is a problem with your Erlang/OTP installation. As the Erlang ODBC application is dependent on third party products there are some notes here:

    This manual could be helpful on how to compile ODBC on unix.

    Also I suggest to take a look at Emysql as a stable third party alternative.