1

This Driver lets you call a mysql database within an Erlang program.

They provide an example for "Multiple Query and Multiple Result sets"

{ok, [{[<<"foo">>], [[42]]}, {[<<"bar">>], [[<<"baz">>]]}]} =
    mysql:query(Pid, "SELECT 42 AS foo; SELECT 'baz' AS bar;"),

But don't really really explain it, or show how to use it without the AS


I would like to execute a query like

{ok, [{[<<Latitude>>, [Longitude], [Image], [Username], [Bio]} = mysql:query(Db, "SELECT * FROM users",[])

This is my regular select * from users

mysql> select * from users;

+----------+-----------+-------+----------+-----------+
| latitude | longitude | image | username | bio       |
+----------+-----------+-------+----------+-----------+
| 44       | 44        |    45 | Sally    | Hello     |
| 42       | 43        |     0 | Bryce    | I'm me    |
| 20       | 24        |     0 | Richard  | I'm Great |
| 44       | 45        |     0 | Liz      | Yeah      |
+----------+-----------+-------+----------+-----------+

So I would want the first column stored within the variable Latitude, the second column stored in Longitude .....

1 Answer 1

1

From the library's example:

%% Select
{ok, ColumnNames, Rows} =
    mysql:query(Pid, <<"SELECT * FROM mytable WHERE id = ?">>, [1]),

You should be able to do it with the following query, with only 2 params on the query function call:

{ok, [<<"latitude">>, <<"longitude">>, <<"image">>, <<"username">>, <<"bio">>], Rows} 
    = mysql:query(Pid, <<"SELECT * FROM users">>),

Where Rows would be like this (based on the table you posted):

[ 
    [44, 44, 45,<<"Sally">>, <<"Hello">>],
    [42, 43, 0,<<"Bryce">>, <<"I'm me">>],
    [20, 24, 0,<<"Richard">>, <<"Hello">>],
    [44, 45, 0,<<"Liz">>, <<"Yeah">>]
]

Each list item in the above structure is a row from your DB table, field values in the same order as your select would request it (as you can see it from the ColumnNames list). You can use this list to transform it to whatever other structure you may need.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.