Solidity:在智能合约中插入、读取和删除数据的正确方法
描述:
我创建了一个简单的智能合约,使用 enterAutomobiles()
函数在其中输入基本信息。为了读取数据,我使用 getAllAutomobiles() 函数。要删除数据,我将使用此函数 removeAutomobile(uint i)
。
问题:
- 使用
getAllAutomobiles()
函数检索数据时,检索到的数据是 unicode 字符的混合形式,如下所示:``` Àà Àà à 111test123test123
问题:为什么会出现上述unicode字符?
问题:我如何呈现以这种格式显示的数据?
[ { vinNumber:1,miscId:“11”,licenseInfo:“testtest”,vehicleDescription:“testtest123”} ];
- 当我想要时使用
removeAutomobile(uint index)
函数删除数据,其中index = 0,即删除index = 0处的数据。但是,当我再次调用removeAutomobile(uint index)
where index = 0 时,没有错误消息表明该索引之前已被删除。这种情况我能做什么?
struct AllAutomobiles {
uint carId;
uint vinNumber;
string miscId;
string licenseInfo;
string vehicleDescription;
}
AllAutomobiles [] public cars;
uint public autoMobileCount = 0;
//Enter automobiles
function enterAutomobiles(uint vinNumber, string memory miscId, string memory licenseInfo, string memory vehicleDescription) public onlyOwner {
cars.push(AllAutomobiles(autoMobileCount, vinNumber, miscId, licenseInfo, vehicleDescription));
autoMobileCount++;
}
//get all automobiles
function getAllAutomobiles() public view returns (AllAutomobiles[] memory) {
return cars;
}
function removeAutomobiles(uint index) public {
if (index >= cars.length) return revert('Automobile does not exist!');
if (index == cars[index].carId) {
delete cars[index];
autoMobileCount--;
}
}
Description:
I've created a simple smart contract where I'd enter in basic information using the enterAutomobiles()
function. To read the data, I use getAllAutomobiles()
function. To remove the data, I'd use the this function removeAutomobile(uint i)
.
Issue:
- When data is retrieved using the
getAllAutomobiles()
function, the resulting retrieved data is a mixture of unicode characters like the following: ``` Àà Àà à 111test123test123
Question: Why is the above unicode characters occurring?
Question: How can I present the data to appear in this format?
[ { vinNumber: 1, miscId: "11", licenseInfo: "testtest", vehicleDescription: "testtest123" } ];
- When I want to remove data using the
removeAutomobile(uint index)
function, where index = 0, the data at index = 0 is deleted. However, when I invoke theremoveAutomobile(uint index)
where index = 0 again, there was no error message indicating that the index was previously removed. What can I do in this case?
struct AllAutomobiles {
uint carId;
uint vinNumber;
string miscId;
string licenseInfo;
string vehicleDescription;
}
AllAutomobiles [] public cars;
uint public autoMobileCount = 0;
//Enter automobiles
function enterAutomobiles(uint vinNumber, string memory miscId, string memory licenseInfo, string memory vehicleDescription) public onlyOwner {
cars.push(AllAutomobiles(autoMobileCount, vinNumber, miscId, licenseInfo, vehicleDescription));
autoMobileCount++;
}
//get all automobiles
function getAllAutomobiles() public view returns (AllAutomobiles[] memory) {
return cars;
}
function removeAutomobiles(uint index) public {
if (index >= cars.length) return revert('Automobile does not exist!');
if (index == cars[index].carId) {
delete cars[index];
autoMobileCount--;
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的 EnterAutomobiles 函数是错误的。您必须在数组中推送“AllAtomobiles”结构,而不是“GiveawayWinners”!这就是 getAllAutomobiles 函数返回奇怪字符的原因。
顺便说一句,根据我的经验,我只能建议您将结构名称更改为更清晰的名称!
编辑:当运行你的新代码时,一切对我来说似乎都很好:
Your enterAutomobiles function is wrong. You have to push an "AllAtomobiles" struct in your array, not an "GiveawayWinners"! That's why the getAllAutomobiles function is returning strange characters.
And by the way, in my experience, I can only advise you to change your struct name to something more clear !
Edit: When running your new code everything seems good to me :