0
pragma solidity ^0.4.23;

contract tracking  {

  struct  data  {
    uint   ownernumber;
    uint   id;
  }

  uint value;
  mapping (uint256 => data) public  datamatching;

  function storedata (uint _ownernumber, uint _id) public {

    var  persondata  = datamatching[value];
    persondata .ownernumber = _ownernumber;
    persondata. id  =  _id;

  }

  function getData(uint256 userId) returns (uint){

   }
}

1) I wrote a function to store the values in my solidity code

2) Now in getData function, I want the user to enter the ownernumber and get all the details matching to the ownernumber is it possible please help me

Common errors:

Use of the "var" keyword is disallowed. Data location must be

"storage" or "memory" for variable, but none was given

0

You have two options to do this.

  1. You can return multiple variables from a function, like this:

    pragma solidity ^0.4.23;
    
    contract tracking  {
    
      struct  data  {
        uint   ownernumber;
        uint   id;
      }
    
      uint value;
      mapping (uint256 => data) public  datamatching;
    
      function storedata (uint _ownernumber, uint _id) public {
        var  persondata  = datamatching[value];
        persondata .ownernumber = _ownernumber;
        persondata. id  =  _id;
      }
    
      function getData(uint256 userId) returns (uint, uint){
        return (datamatching[userId].ownernumber, datamatching[userId].id);
      }
    }
    
  2. Alternatively you can use the experimental v2 abi encoder that allows returning structs from functions, like this:

    pragma solidity ^0.4.23;
    pragma experimental ABIEncoderV2;
    
    contract tracking  {
    
      struct  data  {
        uint   ownernumber;
        uint   id;
      }
    
      uint value;
      mapping (uint256 => data) public  datamatching;
    
      function storedata (uint _ownernumber, uint _id) public {
        var  persondata  = datamatching[value];
        persondata .ownernumber = _ownernumber;
        persondata. id  =  _id;
      }
    
      function getData(uint256 userId) returns (data){
        return datamatching[userId];
      }
    }
    
| improve this answer | |
0

It is not possible to retrieve data by ownernumber as there is no way to iterate mappings unless you know the keys.

You can store all the all the keys of the mappings in an array so that you can iterate mappings and retrieve data for specific ownernumber. The time complexity of this approach will be O(N).

I am assuming there is one to many relationship among owner and data. That is one owner is associated with many data. If it is so then you can reduce the time complexity of retrieving data by owner to O(1). Refer the folowing blog for more details -

https://medium.com/@robhitchens/enforcing-referential-integrity-in-ethereum-smart-contracts-a9ab1427ff42

I think you need to pass value as an argument to storedata function. Let me know if any further details are required.

Hopr it helps.

| improve this answer | |
0

A sample program:

pragma solidity ^0.5.2;

contract bank{
struct Person{
    uint id;
    bytes32 name;
}
mapping(address => Person) public  p;

function getDetails(address a) public view returns(uint ,bytes32){
     Person storage person = p[a];
    return(person.id , person.name);
}
function setDetails(address a,uint id , bytes32 name) public{
    Person storage person = p[a];
    person.id = id;
    person.name = name;
}
}

Note: Now, if you use var keyword like var person = p[a]; , you will get below SyntaxError:

SyntaxError: Use of the "var" keyword is disallowed. Use explicit declaration `struct bank.Person storage pointer person = ...´ instead.

If you use Person person = p[a]; , then you will get TypeError :

TypeError: Data location must be "storage" or "memory" for variable, but none was given. Person person = p[a];

So better Option is to use storage keyword along with Struct name like below:

Person storage person = p[a];
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.