如何将结构转换为智能合约的数组?

发布于 2025-01-20 20:25:20 字数 911 浏览 2 评论 0原文

我将写一份有关患者病历的智能合同。我有一个例子。但是所有存储在结构中的数据。我想使用时间序列数据。据知道,我必须使用一系列结构,但是我不知道该怎么做?

你能帮我吗?

contract MedicalHistory {
    enum Gender {
        Male,
        Female
    }
    uint _patientCount = 0;

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        uint256 createdAt; // save all history
        Gender gender;
    }

    mapping(uint => Patient) _patients;

    function Register(
        string memory name,
        uint16 age,
        string memory telephone,
        string memory homeAddress,
        uint64 birthday,
        string memory disease,
        // uint256 createdAt,
        Gender gender
    }
}

这是我智能合约中的代码段。如何将结构转换为数组?

I will write a smart contract about patient medical records. And I have an example for this. But all data stored in struct. I want to use time-series data. As far as know, I have to use array of struct, but I don't know how can i do that?

Can you help me please?

contract MedicalHistory {
    enum Gender {
        Male,
        Female
    }
    uint _patientCount = 0;

    struct Patient {
        string name;
        uint16 age;
        //max of uint16 is 4096
        //if we use uint8 the max is uint8
        string telephone;
        string homeAddress;
        uint64 birthday; //unix time
        string disease; //disease can be enum
        uint256 createdAt; // save all history
        Gender gender;
    }

    mapping(uint => Patient) _patients;

    function Register(
        string memory name,
        uint16 age,
        string memory telephone,
        string memory homeAddress,
        uint64 birthday,
        string memory disease,
        // uint256 createdAt,
        Gender gender
    }
}

This is the code snippet from my smart contract.. How can I convert struct to array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2025-01-27 20:25:20

您可以.push()到存储数组中,有效地添加新项目。

我简化了代码示例,以便更容易地看到实际的数组操作:

pragma solidity ^0.8;

contract MedicalHistory {
    struct Patient {
        string name;
        uint16 age;
    }

    Patient[] _patients;

    function Register(
        string memory name,
        uint16 age
    ) external {
        Patient memory patient = Patient(name, age);
        _patients.push(patient);
    }
}

请注意,如果您使用的是以太坊等公共网络,则所有存储的数据都是可检索的,即使它存储在非公共网络中 属性通过查询合约存储槽位来实现。有关代码示例,请参阅此答案。因此,除非这只是一个学术练习,否则我真的不建议在区块链上存储健康和其他敏感数据。

You can .push() into a storage array, effectively adding new item.

I simplified the code example just so it's easier to see the actual array manipulation:

pragma solidity ^0.8;

contract MedicalHistory {
    struct Patient {
        string name;
        uint16 age;
    }

    Patient[] _patients;

    function Register(
        string memory name,
        uint16 age
    ) external {
        Patient memory patient = Patient(name, age);
        _patients.push(patient);
    }
}

Please note that if you're using a public network such as Ethereum, all stored data is retrievable even though it's stored in a non-public property by querying the contract storage slots. See this answer for a code example. So unless this is just an academic exercise, I really don't recommend storing health and other sensitive data on the blockchain.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文