从 mnesia 获取字段

发布于 2024-11-02 21:30:27 字数 322 浏览 1 评论 0原文

我有一个 mnesia 表 users ,其中包含用户和密码字段。

我的表中的数据:

[{users, <<"user_name">>, <<"password">>}].

我需要通过用户名获取密码。我做:

mnesia:dirty_read({users, <<"user_name">>}).

但它返回[]

如何通过用户名获取密码?

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

不乱于心 2024-11-09 21:30:27

您没有指定正在使用的记录语法,但它看起来像

-record(users, {用户名, 密码})。

...或类似的东西。那么,假设,当您创建表时,您是否执行了任何特殊操作来设置 id?在此示例中,“用户名”(用户记录中的第一个条目)默认应为 id,除非您做了特殊的操作。

如果问题仍然存在,请考虑使用 mnesia:match_object/1 或 /3。您在模式/记录语法中指定必须匹配的部分(在本例中为用户名),并使用 ='' 来匹配您不知道或不关心的任何内容(在本例中为,这就是密码部分)。

我希望这有帮助!

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!

戴着白色围巾的女孩 2024-11-09 21:30:27

您可以执行如下操作:

YourPasswd = mnesia:ets(fun()-> mnesia:dirty_read({users,UserId}) end),

    case YourPasswd of
            [] -> {error, 'No User Found'};
            [{users,_UserID,Passwd}] ->
                {success, Passwd}
    end.

希望数据能正确写入 mnesia :)

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 :)

○闲身 2024-11-09 21:30:27

看看这个函数:

-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).


这会给你结果。 mnesia:activity/4 的好处是,无论表是否碎片化,答案都可以。
祝你好运

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

初见 2024-11-09 21:30:27

我理解您希望尽快检查您的密码,但是您的项目是否处于需要 优化这个?

我的一个项目中有一个身份验证模块,并且我有与您类似的目标。由于我还没有机会进行优化,因此我正在使用 mnsesia 事务以及用户名和密码列表。

这是我的身份验证模块的一部分。

-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.

编译并启动 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>

为了看看我是否遇到与您相同的问题,我切换到二进制值并执行脏读。

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.

erl shell 中的以下命令表明它按照您期望的方式工作。

12> auth:add_user(<<"rodericktaylor">>, <<"binarypassword">>).            
{atomic,ok}
14> true = auth:start_session_dirty(<<"rodericktaylor">>, <<"binarypassword">>).
true
15>

我希望我有所帮助。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文