【问题标题】:How to Generate AUTO INCREMENT Id in Erlang Mnesia如何在 Erlang Mnesia 中生成 AUTO INCREMENT Id
【发布时间】:2019-06-04 15:04:19
【问题描述】:

我正在尝试将记录写入 mnesia,并且我想为每条记录分配一个唯一的 ID。 id 需要自动递增。

例如,我有一个包含 3 列的表 - Id、Name、DOB。

记录 1 的 ID = 1,姓名 = Joe,出生日期 = 03-05-1994

记录 2 应该有 Id = 2、Name = Alice、DOB = 11-07-1991 等等...

请帮忙

【问题讨论】:

    标签: database erlang elixir mnesia


    【解决方案1】:

    看来您必须手动跟踪最后一个 id。您可以创建一个table_ids 表,该表跟踪在给定表中插入记录时使用的最后一个 id。 table_ids 表可用于单独跟踪所有不同表中使用的最后一个 id。

    根据 mnesia 文档,而不是通过从 table_ids 表中读取表的最后一个 id,然后递增最后一个 id,然后将最后一个 id 写回 table_ids 表来自己进行数学运算,而是您只需致电mnesia:dirty_update_counter()。这是一个例子:

    -module(my).
    -compile(export_all).
    -include_lib("stdlib/include/qlc.hrl"). 
    
    -record(person, {id, name, age}).
    -record(table_id, {table_name, last_id}).
    
    init() ->
        mnesia:create_schema([node()]),
        mnesia:start(),
    
        {atomic, ok} = mnesia:create_table(people, [ 
           {record_name, person},
           {attributes, record_info(fields, person)}
        ]),
    
        {atomic, ok} = mnesia:create_table(table_ids, [
           {record_name, table_id},
           {attributes, record_info(fields, table_id)}
        ]).
    
    start_counter(TableName) ->
        Fun = fun() ->
                    mnesia:write(table_ids, 
                      #table_id{table_name=TableName, last_id=0}, 
                      write
                    )
              end,
        {atomic, ok} = mnesia:transaction(Fun).
    
    
    
    insert(Name, Age) ->
        Index = mnesia:dirty_update_counter(table_ids, people, 1),
        Fun = fun() ->
                    mnesia:write(people,
                        #person{id=Index, name=Name, age=Age},
                        write
                    )
              end,
        {atomic, ok} = mnesia:transaction(Fun).
    
    select_all() -> 
        {atomic, People} = mnesia:transaction(
             fun() ->
                  qlc:eval(
                    qlc:q([X || X <- mnesia:table(people)])
                  )
             end
        ),
        People.
    

    在 iex 中:

    ~/erlang_programs/mnesia$ rm -rf Mnesia.nonode\@nohost/
    
    ~/erlang_programs/mnesia$ erl
    Erlang/OTP 20 [erts-9.3] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:10] [hipe] [kernel-poll:false]
    Eshell V9.3  (abort with ^G)
    
    1> c(my).                   
    my.erl:2: Warning: export_all flag enabled - all functions will be exported
    {ok,my}
    
    2> my:init().               
    {atomic,ok}
    
    3> my:start_counter(people).
    {atomic,ok}
    
    4> my:insert("Joe", 22).    
    {atomic,ok}
    
    5> my:insert("Sue", 31).    
    {atomic,ok}
    
    6> my:select_all().         
    [{person,1,"Joe",22},{person,2,"Sue",31}]
    
    7> mnesia:stop().           
    stopped
    
    8> 
    =INFO REPORT==== 9-Jan-2019::23:30:02 ===
        application: mnesia
        exited: stopped
        type: temporary
    
    (Hit Return to get back to prompt)
    8> 
    

    【讨论】:

      猜你喜欢
      • 2012-03-22
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      • 2022-12-19
      • 1970-01-01
      • 2018-07-03
      • 1970-01-01
      • 2010-10-06
      相关资源
      最近更新 更多