0

I am currently working on little project and got stuck at this matter. So, i created a temp table which is containing item details, what i want is when i press button [save] and it will retrieve those records specific values and put the values into different temp variables.

this is what i did for selecting the specific record values.

DM.Zread.close;
DM.Zread.SQL.Commatext:= 'Select id_dvd from temp_table where rent_id="rent-0001"';
DM.Zread.open;

e.g : selected record values :

id_dvd
| 2 |
| 6 |
| 5 |

as i said before, i wanted to put those record values into 3 different temp variables. so it will be like :

//assumed i did the array function

a:=x[0]; // this line contain value from record one which is 2
b:=x[1]; // this line contain value from record two which is 6
c:=x[2]; // this line contain value from record three which is 5

And i totally have no idea to do this, i've searched on stackoverflow and there isn't any thread discussed about this. But i've got little reference that this methode should be using Array function, that's why i made the example using array. to be honest, i am not really understood using Array.

It will be appreciate for who help me through this.

Thank you

-Dan

1 Answer 1

1

Since you don't know how many rows will be returned by the query you should create a dynamic array to hold the values, then after opening the table, loop over the records and fill the array:

var
  i: integer;
  IDs: array of integer;
begin
  DM.Zread.close;
  DM.Zread.SQL.Text:= 
      Format('Select id_dvd from temp_table where rent_id=%s',[QuotedStr('rent-0001')]);
  DM.Zread.open;

  SetLength(IDs, DM.Zread.RecordCount);
  i:=0;
  while not DM.Zread.Eof do begin
    IDs[i] := DM.Zread.FieldByName('id_dvd').AsInteger;
    inc(i);
    DM.Zread.Next;
  end;
end;
Sign up to request clarification or add additional context in comments.

3 Comments

really appreciate with the answer, this is what i means. this code works fine when i run it, but it give me 'Invalid Pointer Operation' when i press the save button. i've search on stackoverflow. It says "i freed memory that didn't belong to me". What does it mean and how to make them straight ?
I made a change on this lines and it's works fine now. SetLength(IDs, DM.Zread.RecordCount); Am i doing right thing or just messed up and will make another problem future ?
@Dany2014 you did the right change, it should be RecordCount without the (-1)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.