Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<< and >> symbols in Erlang

Tags:

erlang

First of all, I'm an Erlang rookie here. I need to interface with a MySQL database and I found the erlang-mysql-driver. I'm trying that out, and am a little confused by some of the syntax.

I can get a row of data from the database with this (greatly oversimplified for brevity here):

Result = mysql:fetch(P1, ["SELECT column1, column2 FROM table1 WHERE column2='", Key, "'"]),  
case Result of  
    {data, Data} ->  
        case mysql:get_result_rows(Data) of  
            [] -> not_found;  
            Res ->  
              %% Now 'Res' has the row

So now here is an example of what `Res' has:

[[<<"value from column1">>, <<"value from column2">>]]

I get that it's a list of records. In this case, the query returned 1 row of 2 columns.

My question is:
What do the << and >> symbols mean? And what is the best (Erlang-recommended) syntax for turning a list like this into a records which I have defined like:

-record(  
    my_record,  
    {   
      column1 = ""  
      ,column2 = ""  
    }  
 ).     
like image 618
marcc Avatar asked Aug 01 '09 05:08

marcc


2 Answers

Just a small note: the results are not bit string comprehensions per see, they are just bit strings. However you can use bit string comprehensions to produce a sequence of bit strings (which is described above with the generators and that), much like lists and lists comprehensions.

you can use erlang:binary_to_list/1 and erlang:list_to_binary/1 to convert between binary and strings (lists).

The reason the mysql driver returns bit strings is probably because they are much faster to manipulate.

like image 87
Mazen Harake Avatar answered Sep 23 '22 02:09

Mazen Harake


In your specific example, you can do the conversion by matching on the returned column values, and then creating a new record like this:

case mysql:get_result_rows(Data) of
  [] ->
    not_found;  
  [[Col1, Col2]] ->  
    #my_record{column1 = Col1, column2 = Col2}
end
like image 26
Zed Avatar answered Sep 24 '22 02:09

Zed