【三胖聊区块链-EOS】(六)EOS入门系列-智能合约数据持久化之二级索引

501 阅读1分钟

github源码:我在这呢

注意:EOS最多允许表格添加16个索引,表的结构包含数据时无法修改。需要删除已经添加的数据(即清空表),否则在插入数据的时候会报错。

在上一篇:【三胖聊区块链-EOS】(五)EOS入门系列-智能合约数据持久化 的基础上添加二级索引

二级索引必须是数字类型

下面截图有详细说明:

代码片段如下:

 /** 
      TABLE:标签,声明这是一张table
    */
    TABLE person {  //定义一张table,用于存储data
      name key;
      string first_name;
      string last_name;
      uint64_t age;
      string street;
     string city;
     string state;
     auto primary_key() const { return key.value; }  //定义主键
     uint64_t get_secondary_1() const { return age; } //  二级索引,(必须是数字字段)
    };

    /**
      设置为多索引
      name("people")  命名为people 
      person  传入上面定义的表结构
      address_index 类型将用于实例化表
      indexed_by<name("byage"), const_mem_fun<person, uint64_t, &person::get_secondary_1>> : 二级索引

        name("byage"): 第一个参数二级索引名称为byage
        uint64_t:第二个类型参数指定为函数调用运算符
        &person::get_secondary_1:该函数将提取const值作为索引键。在这种情况下,我们将其指向我们先前创建的getter
    */
   typedef multi_index<name("people"), person, indexed_by<name("byage"), const_mem_fun<person, uint64_t, &person::get_secondary_1>>> address_index;

更多

想了解更多,请关注公众号哦!