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.