Question

Iam new in erlang and trying to get data from mysql server:

   <erl>
        out(A) ->
    application:start(odbc), 
    ConnString = 
    "Driver={MySQL ODBC 5.2 ANSI Driver};" ++
    "Server=127.0.0.1;Database=teamsDatabase;" ++ 
        "User=root;Password=1q2w3e;" ++ 
        "Option=3;", 
        {ok, Conn} = odbc:connect(ConnString, []), 
        Results = odbc:sql_query(Conn, "select team_name from teams limit 2"), 
        {ehtml,
            [{h4,[], "The database result:"},
        {hr},
        {html, lists:map(fun(X) -> {Tname} = X, io_lib:format("ID: ~p ", [Tname]) end, Results)}]}.

of course, I get the error:

ERROR erlang code threw an uncaught exception:
 File: c:/yaws/zero.yaws:39
Class: error
Exception: function_clause
Req: {http_request,'GET',{abs_path,"/zero.yaws"},{1,1}}
Stack: [{lists,map,
               [#Fun,
                {selected,["team_name"],[{"Team 1"},{"Team 2"}]}],
               [{file,"lists.erl"},{line,1223}]},

How i can output my data? It`s looks like this:

{selected,["team_name"],[{"Team 1"},{"Team 2"}]}

This is list? or..? I found this: Output data of Erlang List as a HTML in Yaws But it is did not work for me.

Was it helpful?

Solution

The error you're getting is due to passing a tuple rather than a list as the second argument to lists:map/2.

If you're using ehtml to return results, you could do this to just print the whole result as a string:

{ehtml,
 [{h4, [], "The database result:"},
  {hr},
  {p, [], io_lib:format("~p", [Results])}]}

But of course that's less than ideal as it exposes Erlang-formatted terms in your web page. A better approach might be:

{selected, [Selector], Results} = odbc:sql_query(Conn, "select team_name from teams limit 2"),
{ehtml,
 [{h4, [], "The database result:"},
  {hr},
  [{p, [], [Selector, ": ", Val]} || Val <- Results]]}

which results in the following HTML:

<h4>The database result:</h4><hr />
<p>team_name: Team 1</p>
<p>team_name: Team 2</p>

Notice that this second approach pattern-matches the results coming back from the database query to help prepare the results for use in forming the ehtml. You can change the details of this approach to format the results as you wish.

BTW you should use another approach to establishing your database connection, since putting it into the out/1 function of a .yaws page means it's going to be run every time a client requests that page. For something simple you might create a small Erlang application with one supervisor that watches a gen_server that connects to the database, where your gen_server callback module has an API function to return the connection for use in your out/1 function. This application can be started when Yaws starts via the runmod feature described in the Yaws documentation. The nice thing about this is that it can cleanly connect and disconnect from the database during application start and stop respectively, but note that it gives you only a single connection. For a more scalable approach, you can probably find database connection pooling modules on github or other sites that you can use instead.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top