eos 连接mysql_EOS智能合约中数据库的使用与常见问题

阅读本文前,您需要熟悉eos节点的操作流程,熟悉cleos客户端基础指令,并且对自定义合约的开发有着一定的了解。

操作系统:MAC OS 10.13.x,EOSIO版本号:1.1.3

背景

在EOS自定义合约开发过程中有持久化存储的需求,则需要创建一个用作持久化存储的数据库。EOS中的数据库是通过multi_index来完成交互与访问。

下面通过我们先创建数据表,并进行数据表进行增删改查。

1 创建数据表

创建智能合约

在创建数据表前,我们首先需要创建智能合约,关于智能合约的创建,网上有很多这样的教程,文本不再赘述。

可以参考EOSIO3.0 hello world 从 0 到 1

创建数据表结构

//@abi table cactus.db i64

struct cts_db {

uint64_t id;

string store_content;

uint64_t primary_key() const { return id; }

EOSLIB_SERIALIZE(cts_db, (id)(store_content))

};

typedef multi_index cactus_db_index;

//@abi table cactus.db i64 表示生成abi的时候生成name为cactus.db index_type为i64 的表。下面是生成的abi中的表结构。

"tables": [{

"name": "cactus",

"index_type": "i64",

"key_names": [

"id"

],

"key_types": [

"uint64"

],

"type": "cts_db"

}

]

其中有几点注意事项以及可能会由此导致的问题:

//@abi table 生成abi的时候会生成该新的数据表,不加则生成的abi中table无内容,进行查表操作的时候显示如下。

Table cactus.db is not specified in the ABI

在对表名进行命名是要注意,表名长度需要小于13(不包括13)个字符,且只能包含.12345abcdefghijklmnopqrstuvwxyz这些字符,否则查找的时候会提示错误。

一定需要有primary_key()方法。

当//@abi table中声明的表名包括'.'时,生成abi文件时会将'.'后面的内容忽略,可以观察上方生成的abi文件中"name"为"cactus"而不是在代码中所声明的"cactus.db"。

若//@abi table后不写声明表名,生成abi时表名会取结构体的名字。

生成multi_index对象时,multi_index时,table_name需要与上面所声明的table_name一致,否则会造成查表的时候内容为空的情况。

cactus-MacBook-Pro:cactus_db huid$ cleos get table cactus huid cactus

{

"rows": [],

"more": false

}

2 操作简单数据表

获取数据表信息

cleos get table code scope table_name

这里的code与scope与创建实例对象时multi_index(code, scope)的参数相对应,否则获取的时候虽然不会报错,但是没有内容。

数据表插入方法使用

///@abi action

void ctsstore(account_name user){

cactus_db_index ctsdb( _self, user );

ctsdb.emplace( user, [&]( auto& a) {

a.id = ctsdb.available_primary_key();

a.store_content = store_content;

});

}

其中available_primary_key()是一个实用的方法,可以提供主键自增长功能。

执行并获取表信息。

cleos push action cactus ctsstore '{"user":"huid", "store_content":"cactus"}' -p huid@active

cleos push action cactus ctsstore '{"user":"huid", "store_content":"cactus"}' -p huid@active

cleos get table cactus huid cactus

{

"rows": [{

"id": 0,

"user_name": "cactus"

},{

"id": 1,

"user_name": "cactus"

}

],

"more": false

}

数据表查找方法使用

cactus_db_index ctsdb( _self, user );

auto& itr = ctsdb.get( id , "data not found" );

数据表修改方法使用

///@abi action

void ctsmodify( uint64_t id, account_name user, string store_content ){

cactus_db_index ctsdb( _self, user );

auto& itr = ctsdb.get( id , "data not found" );

ctsdb.modify( itr, user, [&]( auto& a ) {

a.store_content = store_content;

});

}

执行并获取表信息。

cleos push action cactus ctsmodify '{"id":1, "user":"huid", "store_content":"db"}' -p huid@active

cleos get table cactus huid cactus

{

"rows": [{

"id": 0,

"user_name": "cactus"

},{

"id": 1,

"user_name": "db"

}

],

"more": false

}

数据表删除方法使用

///@abi action

void ctserase( uint64_t id, account_name user ){

cactus_db_index ctsdb( _self, user );

auto &itr = ctsdb.get( id , "data not found" );

ctsdb.erase( itr );

}

执行并获取表信息。

cleos push action cactus ctserase '{"id":1, "user":"huid"}' -p huid@active

cleos get table cactus huid cactus

{

"rows": [{

"id": 0,

"user_name": "cactus"

}

],

"more": false

}

源代码

#include

using namespace eosio;

using namespace std;

class cactus_db : public contract {

public:

using contract::contract;

///@abi action

void ctsstore( account_name user, string store_content ){

cactus_db_index ctsdb( _self, user );

ctsdb.emplace( user, [&]( auto& a ) {

a.id = ctsdb.available_primary_key();

a.store_content = store_content;

});

}

///@abi action

void ctsmodify( uint64_t id, account_name user, string store_content ){

cactus_db_index ctsdb( _self, user );

auto& itr = ctsdb.get( id , "data not found" );

ctsdb.modify( itr, user, [&]( auto& a ) {

a.store_content = store_content;

});

}

///@abi action

void ctserase( uint64_t id, account_name user){

cactus_db_index ctsdb( _self, user );

auto &itr = ctsdb.get( id , "data not found" );

ctsdb.erase( itr );

}

private:

//@abi table cactus.db i64

struct cts_db {

uint64_t id;

string store_content;

uint64_t primary_key() const { return id; }

EOSLIB_SERIALIZE(cts_db, (id)(store_content))

};

typedef multi_index cactus_db_index;

};

EOSIO_ABI(cactus_db, (ctsstore)(ctsmodify)(ctserase))

3 多索引数据表

多索引表数据表和简单数据表在定义的时候不同。

简单数据表: multi_index

多索引数据表: multi_index

indexed_by,

indexed_by,

….>>

多索引数据表较简单数据表多了一个indexed_by,, 以下具体说明。

创建多索引数据表

private:

//@abi table cactus.db i64

struct cts_db {

uint64_t id;

uint64_t index_id;

string store_content;

uint64_t primary_key() const { return id; }

uint64_t get_index_id() const { return index_id; }

EOSLIB_SERIALIZE(cts_db, (id)(index_id)(store_content))

};

typedef multi_index< N(cactus), cts_db, indexed_by< N(index_id),

const_mem_fun< cts_db, uint64_t, &cts_db::get_index_id > > > cactus_db_index;

};

注意点:

除了主键函数外还需要写一个索引键值函数,如get_index_id()

结构中的<>注意成对。

多索引数据表查找并修改

cactus_db_index ctsdb( _self, user );

auto idx = ctsdb.template get_index();

auto itr = idx.find(mtrans::get_index_id(index_id));

if ( itr == ctsdb.end() ) { print("don't find"); }

else{

ctsdb.modify( itr, user, [&](auto &a ){

a.store_content = store_content;

});

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值