多索引表 创建多索引表
Posted thefist11
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了多索引表 创建多索引表相关的知识,希望对你有一定的参考价值。
1. 创建表
- class/struct 定义一个持久化对象。
- 该对象需要有一个const的成员作为主键,类型为uint64_t
- 二级主键可选,提供不同的键类型
- 为每个二级索引定义一个键导出器,键导出器是一个函数,可以用来从Multi_index表中获取键
multi_index_example.hpp
#include <eosio/eosio.hpp>
using namespace eosio;
// multi index example contract class
class [[eosio::contract]] multi_index_example : public contract {
public:
using contract::contract;
// contract class constructor
multi_index_example( name receiver, name code, datastream<const char*> ds ) :
// contract base class contructor
contract(receiver, code, ds),
// instantiate multi index instance as data member (find it defined below)
testtab(receiver, receiver.value)
{ }
// the row structure of the multi index table, that is, each row of the table
// will contain an instance of this type of structure
struct [[eosio::table]] test_table {
// this field stores a name for each row of the multi index table
name test_primary;
// additional data stored in table row
uint64_t datum;
// mandatory definition for primary key getter
uint64_t primary_key( ) const { return test_primary.value; }
};
// the multi index type definition, for ease of use define a type alias `test_tables`,
// based on the multi_index template type, parametarized with a random name and
// the test_table data structure
typedef eosio::multi_index<"testtaba"_n, test_table> test_tables;
// the multi index table instance declared as a data member of type test_tables
test_tables testtab;
[[eosio::action]] void set( name user );
[[eosio::action]] void print( name user );
using set_action = action_wrapper<"set"_n, &multi_index_example::set>;
using print_action = action_wrapper<"print"_n, &multi_index_example::print>;
};
以上是关于多索引表 创建多索引表的主要内容,如果未能解决你的问题,请参考以下文章