Reputation: 553
I have a users table in mysql with userid, username, password etc Using mysql-otp i query like
select * from users where username = ?
Its returns ColumnNames and Rows. Rows are like
[[38, <<"joe">>, <<"passwordhash">>..]]
Suppose i have a hash to compare
Hash = "passhash".
As its my 3rd day coding in erlang, what i am currently doing/testing is
[[_, _, UserPass, _,..]] = Rows.
Which stores password in UserPass.
Pass = binary_to_list(UserPass).
Which i can then compare like
Hash == Pass.
Is this approach correct or i am doing it all wrong? There must be a proper way of getting data out of what is supposedly list inside a list.
Upvotes: 0
Views: 112
Reputation: 48599
There must be a proper way of getting data out of what is supposedly list inside a list
The proper way to get at your target data is to use pattern matching to deconstruct whatever type of collection contains your data. Because mysql-otp
returns a list of rows matching the query, where each row itself is a list, the data is in the form of a list of lists. Therefore, in order to match a list of lists your pattern also has to be a list of lists.
As its my 3rd day coding in erlang, what i am currently doing/testing is
[[_, _, UserPass, _,..]] = Rows.
Bravo.
If you know mysql-otp
will only return one row, or you are only interested in the first row, you could simplify the pattern like this:
[_, _, UserPass, _, ...] = hd(Rows).
hd
is shorthand for head, i.e. the head of the list. Or, you could accomplish the same thing by hand like this:
[FirstRow | _Tail] = Rows,
[_, _, UserPass, _, ...] = FirstRow.
=======
Another way to extract the password:
UserPass = lists:nth(3, hd(Rows)).
Upvotes: 1