1. table的entry循环赋值出错
执行代码
pragma solidity ^0.4.25; struct Bean { // 表名称 string tableName; // 主键 string primaryKey; // 表字段 string[] fields; } Bean t_bag_struct; for (uint j = 0; j < 12; j++) { entry.set(t_bag_struct.fields[j], fields[j]); }
错误描述
查看entry的值,出i=0,之后值均为空。
错误原因
字符串解析方法(如下代码所示)中,切割时依照“,”作为分隔符
strings.slice memory s = _fields.toSlice(); strings.slice memory delim = ",".toSlice(); uint params_total = s.count(delim) + 1; t_bag_struct.fields = new bytes32[](params_total); for(uint i = 0; i < params_total; i++) { t_bag_struct.fields[i] = stringToBytes32(s.split(delim).toString()) ; }
但之前待分隔字符串中“,”附近存在空格:
string constant tableBagFields = "userID, bag_name, production_date, brand, style, bag_status, activate_time, original_price, current_estimated_price, transfer_times, thumb_up_times, punch_locations_times";
因此,t_bag_struct.fields中的字符串会存在若干个空格问题,导致entry.set无法存储
修改之后(去掉空格),代码如下,运行正常。
string memory tableBagFields = "userID,bag_name,production_date,brand,style,bag_status,activate_time,original_price,current_estimated_price,transfer_times,thumb_up_times,punch_locations_times";
2. 非storage变量不能使用push,memory变长数组不支持push
执行代码
string[12] memory args = ["620523199x","jipinbao","1998-03-13","LV","oldschool","1","2020-05-28","20000","35000","5","1821","11"]; string[] memory inputs = new string[](args.length); // 非storage变量不能使用push,memory变长数组不支持push() for(uint i=0; i<args.length;i++){ inputs[i] = args[i]; }
pragma solidity ^0.4.0; contract AutoExtendArray{ uint[] stateVar = new uint[](1); function autoExendArray(uint a) returns (uint){ //stateVar.length++语句会修改数组的长度加1 stateVar[stateVar.length++] = a; return stateVar[stateVar.length - 1]; } }
在上面这个例子中,可以看到,可通过stateVar.length++语句对数组长度进行自增,我们就得到了一个不断变长的数组。
3. string[]作为函数参数,似乎无法成功
改为string,然后使用strings库进行切
4. 处理时间函数
https://github.com/bokkypoobah/BokkyPooBahsDateTimeLibrary
5.Table插入问题
一旦出问题,重新命名表并新建。