从 mnesia 获取字段
我有一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您没有指定正在使用的记录语法,但它看起来像
-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!
您可以执行如下操作:
希望数据能正确写入 mnesia :)
You can do something like below:
Hope data is correctly written into mnesia :)
看看这个函数:
这会给你结果。 mnesia:activity/4 的好处是,无论表是否碎片化,答案都可以。
祝你好运
look at this function:
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
我理解您希望尽快检查您的密码,但是您的项目是否处于需要 优化这个?
我的一个项目中有一个身份验证模块,并且我有与您类似的目标。由于我还没有机会进行优化,因此我正在使用 mnsesia 事务以及用户名和密码列表。
这是我的身份验证模块的一部分。
编译并启动 erlang shell 后。
为了看看我是否遇到与您相同的问题,我切换到二进制值并执行脏读。
erl shell 中的以下命令表明它按照您期望的方式工作。
我希望我有所帮助。
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.
After compiling and starting up the erlang shell.
To see if I get the same issue you are having, I switched to binary values and did dirty reads.
The following commands in the erl shell show that It works how you expect it to.
I hope I've helped.