tags:

views:

692

answers:

1

Just wondering if anyone could give a working example of using the erlang-mysql module (http://code.google.com/p/erlang-mysql-driver/).

I am new to erlang and I am trying to replace some old scripts with a few erlang batch processes. I am able to connect to the DB and even complete a query, but I am not sure how I use the results. Here is what I currently have:

-include("../include/mysql.hrl").
...
mysql:start_link(p1, "IP-ADDRESS", "erlang", "PASSWORD", "DATABASE"),
Result1 = mysql:fetch(p1, <<"SELECT * FROM users">>),
io:format("Result1: ~p~n", [Result1]),
...

I also have a prepared statement that I am also using to get just one row (if it exists) and it would be helpful to know how to access the results on that as well

+1  A: 

This is described in the source code of mysql.erl:

Your result will be {data, MySQLRes}.

FieldInfo = mysql:get_result_field_info(MysqlRes), where FieldInfo is a list of {Table, Field, Length, Name} tuples.

AllRows = mysql:get_result_rows(MysqlRes), where AllRows is a list of lists, each representing a row.

Zed
Thanks, As an addon to this:What is the best way to parse the result? Sometimes it will result in 0 rows or sometimes there will be lots of rows. In that light if I needed to grab the email address and the name (from the user table, assume structure is [id, email, name]). Some sample code for that would be really great.
Matt
`[do_sg(Email, Name) || [_,Email,Name] <- AllRows]`. btw if you don't need ID you should not query it.
Zed