您当前的位置: 首页 > 
  • 0浏览

    0关注

    1477博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

FISCO BCOS Solidity 智能合约 return string[] This type is only supported in the new experimental ABI

软件工程小施同学 发布时间:2020-09-27 19:16:06 ,浏览量:0

最开始引入:

pragma experimental ABIEncoderV2;

 

pragma solidity ^0.4.25;
pragma experimental ABIEncoderV2;

// import "./Table.sol";
 
contract StudentScoreByCRUD{
 
	address private _owner;
 
	modifier onlyOwner{
		require(_owner == msg.sender, "Auth: only owner is authorized");
		_;
	}
 
	constructor () public {
		_owner = msg.sender;
	}
 
 
 
	event createEvent(address owner, string tableName);
	event insertEvent(address studentId, string courseName, int score);
	event updateEvent(address studentId, string courseName, int score);
	event removeEvent(address studentId, string courseName);
 
 
 
 
	// 创建成绩表
	function create() public onlyOwner returns(int){
 
		TableFactory tf = TableFactory(0x1001);
		int count = tf.createTable("stu_score", "student_id", "course_name, score");
		emit createEvent(msg.sender, "stu_score");
		return count;
 
	}
 
 
	// 插入成绩操作
	function insert(address studentId, string courseName, int score) public onlyOwner returns(int){
 
 
		TableFactory tf = TableFactory(0x1001);
		Table table = tf.openTable("stu_score");
 
		string memory stuIdStr = addressToString(studentId);
		Entry entry = table.newEntry();
		entry.set("student_id", stuIdStr);
		entry.set("course_name", courseName);
		entry.set("score", score);
 
		int count = table.insert(stuIdStr, entry);
		emit insertEvent(studentId, courseName, score);
		return count;
 
	}
 
 
	function addressToString(address addr) private pure returns(string) {
		// Convert addr to bytes
		bytes20 value = bytes20(uint160(addr));
		bytes memory strBytes = new bytes(42);
		// Encode hex prefix
		strBytes[0] = '0';
		strBytes[1] = 'x';
 
		// Encode bytes usig hex encoding
		for(uint i = 0; i < 20; i++){
			uint8 byteValue = uint8(value[i]);
			strBytes[2 + (i 4);
			strBytes[3 + (i= 0 && num  a-f
		return byte(num + 87);
	}
 
 
	// 更新成绩操作
	function update(address studentId, string courseName, int newScore) public onlyOwner returns(int){
 
		TableFactory tf = TableFactory(0x1001);
		Table table = tf.openTable("stu_score");
 
		Entry entry = table.newEntry();
		entry.set("score", newScore);
 
		string memory stuIdStr = addressToString(studentId);
		Condition condition = table.newCondition();
		condition.EQ("student_id", stuIdStr);
		condition.EQ("course_name", courseName);
 
		int count = table.update(stuIdStr, entry, condition);
		emit updateEvent(studentId, courseName, newScore);
 
		return count;
 
	}
 
	// 删除成绩操作
	function remove(address studentId, string courseName) public onlyOwner returns(int){
 
		TableFactory tf = TableFactory(0x1001);
		Table table = tf.openTable("stu_score");
 
		string memory stuIdStr = addressToString(studentId);
		Condition condition = table.newCondition();
		condition.EQ("student_id", stuIdStr);
		condition.EQ("course_name", courseName);
 
		int count = table.remove(stuIdStr, condition);
		emit removeEvent(studentId, courseName);
 
		return count;
 
	}
 
	// 查询成绩操作
	function select(address studentId, string courseName) public view returns(int, string){
 
		TableFactory tf = TableFactory(0x1001);
		Table table = tf.openTable("stu_score");
 
		string memory stuIdStr = addressToString(studentId);
		Condition condition = table.newCondition();
		condition.EQ("student_id", stuIdStr);
		condition.EQ("course_name", courseName);
 
		Entries entries = table.select(stuIdStr, condition);
 
		if(entries.size() == 0){
		    return (0, stuIdStr);
		}
		else{
		    
		    return (entries.get(0).getInt("score"), stuIdStr);
		}
 
	}
	
	// 查询某个学生的某课成绩所有成绩
	function select_all_scores(address studentId, string courseName) public view returns(int[]){
 
		TableFactory tf = TableFactory(0x1001);
		Table table = tf.openTable("stu_score");
 
		string memory stuIdStr = addressToString(studentId);
		Condition condition = table.newCondition();
		condition.EQ("student_id", stuIdStr);
		condition.EQ("course_name", courseName);
 
		Entries entries = table.select(stuIdStr, condition);
 
        // 初始化数组大小
        int[] memory user_scores_list = new int[](uint256(entries.size()));
        
        // 给数组赋值
		for(int i=0; i            
关注
打赏
1665320866
查看更多评论
0.0524s