I have a mnesia table users
with user and password field.
Data from my table:开发者_开发技巧
[{users, <<"user_name">>, <<"password">>}].
I need to get password by user name. I make:
mnesia:dirty_read({users, <<"user_name">>}).
But it returns []
.
How can I get the password by user name?
You didn't specify the record syntax you are using, but it looks like
-record(users, {username, password}).
... or something similar. So, assuming that, when you created the table, did you do anything special to set the id? In this example "username" (the first entry in the users record) should be the id by default, unless you did something special.
If you continue to have issues, consider using mnesia:match_object/1 or /3. You specify, in pattern / record syntax the portion you have to match (in this case, the username), and use ='' to match anything you don't know or care about (in this case, that'd be the password portion).
I hope that helps!
You can do something like below:
YourPasswd = mnesia:ets(fun()-> mnesia:dirty_read({users,UserId}) end), case YourPasswd of [] -> {error, 'No User Found'}; [{users,_UserID,Passwd}] -> {success, Passwd} end.
Hope data is correctly written into mnesia :)
look at this function:
-define(TABLE_NAME,users). get_password_by_username(Username)-> F = fun(U)-> mnesia:read({?TABLE_NAME,U}) end, mnesia:activity(transaction,F,[Username],mnesia_frag).
That will give you the result. The good thing with mnesia:activity/4 is that wether the table is fragmented or not, the answers are okay. good luck
I understand you want your passwords checked as quickly as possible, but is your project at the stage where you need to optimize this?
I've an authentication module in one of my projects and I have a similar goals as yours. As I haven't had occasion to optimize just yet, I am using mnsesia transactions and lists for user names and passwords.
Here is part of my auth module.
-module(auth).
-export([init/1, add_user/2, start_session/2]).
-record(auth_user, {username, password}).
init(Nodes) ->
mnesia:create_table(auth_user,
[{disc_copies, Nodes},
{attributes, record_info(fields, auth_user)}]).
add_user(Username, Password) ->
T = fun() ->
mnesia:write(#auth_user {
username = Username,
password = Password})
end,
mnesia:transaction(T).
start_session(Username, Password) ->
T = fun() ->
mnesia:read(auth_user, Username)
end,
{atomic, Ret} = mnesia:transaction(T),
case Ret of
[U] ->
if (U#auth_user.password == Password) ->
true;
true ->
false
end;
_Else ->
false
end.
After compiling and starting up the erlang shell.
Eshell V5.8.3 (abort with ^G)
1> mnesia:create_schema([node()]).
ok
3> mnesia:start().
ok
4> auth:init([node()]).
{atomic,ok}
5> auth:add_user("rodericktaylor", "password").
{atomic,ok}
6> true = auth:start_session("rodericktaylor", "password").
true
7>
To see if I get the same issue you are having, I switched to binary values and did dirty reads.
start_session_dirty(Username, Password) ->
case mnesia:dirty_read(auth_user, Username) of
[U] ->
if (U#auth_user.password == Password) ->
true;
true ->
false
end;
_Else ->
false
end.
The following commands in the erl shell show that It works how you expect it to.
12> auth:add_user(<<"rodericktaylor">>, <<"binarypassword">>).
{atomic,ok}
14> true = auth:start_session_dirty(<<"rodericktaylor">>, <<"binarypassword">>).
true
15>
I hope I've helped.
精彩评论