FISCO table表使用

Last updated on 21 hours ago

介绍

fico Table.sol提供了CRUD接口用于直接访问底层的存储表

使用

在节点控制台中的合约仓库中,可以添加Table.sol到IDE
引用Table.sol

1
import "Table.sol";

Table.sol的接口包括:

  • createTable :创建表
  • select(string, Condition): 查询数据
  • insert(string, Entry): 插入数据
  • update(string, Entry, Condition): 更新数据
  • remove(string, Condition): 删除数据

创建表

1
2
3
4
5
6
7
// TableFactory的地址固定为0x1001
TableFactory tf = TableFactory(0x1001);

int count = tf.createTable("table_name", "key_name", "item_1,item_2");
if (count == 0) {
// success
}

解释:

  • TableFactory(0x1001):
    TableFactory合约默认地址为0x1001

  • table_name:
    创建的表的表名

  • key_name:
    表的主键名,在Table中,可以有多个相同主键

  • item_1,item_2:
    表的字段名,用逗号分隔

  • count:
    createTable会返回状态码,为0则创建成功,状态码如下:

    错误码 说明
    0 创建成功
    -50000 用户没有权限
    -50001 创建表名已存在
    -50002 表名超过48字符
    -50003 valueField长度超过64字符
    -50004 valueField总长度超过1024字符
    -50005 keyField长度超过64字符
    -50007 存在重复字段
    -50007 字段存在非法字符
    其他 创建时遇到的其他错误

导入表

1
2
3
4
5
6
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("table_name");

if(table == address(0x0)) {
// error
}

解释:

  • tf.openTable("table_name"):
    table_name为表名,返回table_name的地址,如果address(0x0)则表示打开失败

插入行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("table_name");

// 新建行
Entry entry = table.newEntry();
// 设置字段值
entry.set("item_1","1");
entry.set("item_2","2");
// 将行插入到表中,主键设为key1
int count = table.insert("key1", entry);

if (count == 1) {
// success
}

解释:

  • Entry:
    Entry表示一行,table.newEntry用于新建一个table表对应的行
  • set:
    用于设置行的字段,第一个参数为字段名,第二个参数为字值,字段值支持int string address
  • insert:
    用于将行插入到表中,第一个参数为插入行的主键值,第二参数为待插入的行对象
  • count:
    insert返回值表示 “受影响的行数” 为1说明插入成功

查询

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("table_name");
if(table == address(0x0)) {
// error
}

// 新建筛选条件对象
Condition condition = table.newCondition();
// 筛选出符合条件的行
Entries entries = table.select("key1", condition);

// 新建数据,存储数据,根据字段选择数组类型
bytes32[] string_item_list = new bytes32[];
int256[] int_item_list = new int256[];

// 获得行数
int size = entries.size();
// 便利每行,将对应数据放入数组
for (int i = 0; i < size; i++) {
// 获取查询结果的第i行
Entry entry = entries.get(i);
// 获取i行的item_1字段,以Bytes32的形式读取
string_item_list[uint256(i)] = entry.getBytes32("item_1");
// 获取i行的item_1字段,以int的形式读取
int_item_list[uint256(i)] = entry.getInt("item_2");
}

解释:

  • Entries:
    Entry的复数,表示多行的集合

  • Condition:
    过滤条件对象,可以通过它的子方法添加过滤条件,condition.EQ("item_1","1")表示筛选条件为 字段item_值 为 1的行,条件可以设置多个

    1
    2
    condition.EQ("item_1","1");
    condition.EQ("item_1","2");
    接口 功能 参数
    EQ(string, int) 相等条件 字段名,字段值
    EQ(string, string) 相等条件 字段名,字段值
    NE(string, int) 不等条件 字段名,字段值
    NE(string, string) 不等条件 字段名,字段值
    GT(string, int) 大于条件 字段名,字段值
    GE(string, int) 大于或等于条件 字段名,字段值
    LT(string, int) 小于条件 字段名,字段值
    LE(string, int) 小于或等于条件 字段名,字段值
    limit(int) 记录选取条件 返回多少条记录
    limit(int, int) 记录选取条件 记录启始行位置,返回多少条记录
  • table.select:
    用于筛选数据,参数一为主键值,参数二为筛选条件,比table.select("key1", condition)表示筛选出所有主键为key1且,满足筛选条件condition的行

  • entries.size:
    返回行数

  • entries.get:
    单独获取某一行,参数为uint,表示需要获取的行

  • entry.get...():
    Entry提供了多个get方法,在使用时需要根据字段值使用应的get方法,如getString getBytes32 getInt,传入参数为需要获取的字段名

    接口 功能 参数
    getInt(string) 获取字段值 字段名
    getString(string) 获取字段值 字段名
    getBytes64(string) 获取字段值 字段名
    getBytes32(string) 获取字段值 字段名
    getAddress(string) 获取字段值 字段名

删除行

1
2
3
4
5
6
7
8
9
10
11
TableFactory tf = TableFactory(0x1001);
Table table = tf.openTable("table_name");
if(table == address(0x01)) {
// error
}

// 新建筛选条件
Condition condition = table.newCondition();
condition.EQ("item_1","1");
// 删除
int count = table.remove("key1", condition);

解释:

  • count:
    删除的行数
  • table.remove:
    删除满足条件的行,参数一为主键值,参数二为筛选条件,返回值为删除的行数

修改行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
TableFactory tf =TableFactory(0x1001);
Table table =tf.openTable("t_test");
// 检查table是否是空地址
if(table == address(0x0))
{
// error
}

// 新建行
Entry newEntry = table.newEntry();
// 设置字段值
entry.set("item_1","1");
entry.set("item_2"."2");

// 新建过滤条件
Condition condition = table.newCondition();
// 设置过滤条件
condition.EQ("imte_1","update");

// 修改行
int count = table.update("key1", newEntry, condition);

解释:

  • update:
    参数一为主键值,参数二为修改后的行对象,参数三为过滤条件,返回值为修改的行数

注意事项:

  1. 主键不唯一
  2. 字段过多,可以使用数组或struct封装参数