Forum > Beginners

Dynamic array empty elements issue

<< < (2/3) > >>

noszone:

--- Quote from: TRon on February 06, 2023, 07:14:30 am ---
I fail to see the connection between 700 database records vs 7000 array elements and what you are trying to copy and what exactly from where to where . Your code-snippets are not very helpful in that regards.

--- End quote ---

Let imagine in db you have 10 employees and they have in certificates tables about 100 documents. So when you doing loop over these records:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---for i... do beginSetlength(myarray,Length(myarray)+1)...retrive data from dbIF condtin .... thenwrite values to array[i]  // current i value can be 230 when IF condtion becomes trueend;
So we have array like [empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,DATA,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,data].

TRon:
Thank you for the additional information/code noszone.


--- Quote from: noszone on February 06, 2023, 09:30:13 am ---As you can see, here I've used an increment, so no holes in arrays(the program now working 100%, but I want to know why with empty elements it doesn't work properly).

--- End quote ---
That depends on what empty elements you meant exactly. Are you referring to SelEmp and SelItems ?

And what do you mean exactly with "it does not work properly" ?

Are you referring to your result parameter DocumentDataType, your cert_d array (is that a global variable perhaps because it is missing from your code-snippets ?) or are you referring to (visual) results with regards to your datasheet ?


--- Quote from: noszone on February 06, 2023, 09:44:18 am ---Let imagine in db you have 10 employees and they have in certificates tables about 100 documents. So when you doing loop over these records:
 <snip>
So we have array like [empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,DATA,empty,empty,empty,empty,empty,empty,empty,empty,empty,empty,data].

--- End quote ---
Yeah, I understand now but what I do not understand if that is the behaviour you want/seek or if those are the empty elements you wish to know of as of why they are there.


Please keep in mind that we are not mind-readers. We do not have the same source-code in front of us (only some small snippets) We do not have the same data from the database that you use, no fpworksheet and certainly not your mindset. Nobody in his/her right mind wil try to replicate and/or fill in the missing bits to try and replicate what you have in front of you.

So if you have questions or explanations then please be specific in your wording.

dseligo:
You code looks ok, but it is hard to be sure from code snippets. Maybe you have error in some code you didn't show.
You can prepare and post test data which shows problem (of course, with changed names and other sensitive data).
Or you'll have to debug it step by step.
Find employee who misses data in Excel. Then see what certificates he should have. Then go step by step for just that employee and see where data start "missing".

One more thing.
I don't know if you use this data somewhere else, but if you have all this data in database maybe you don't need to fill all these arrays.
For me it would be much simpler to select data from database, prepared for excel.

Something like that:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---var LastEmpID: Integer = -1;...MyWorksheet.WriteText('...write headers...');// if there are employees without license and you need to show them, then you need// left outer joinWith SQLQuery5 do begin  SQL.Text := 'select emp.id as empid, emp.surname, emp.forename, emp.position, ' +                  '       lic.id as licid, lic.description, ' +                  '       emplic.ISSUEDDATE, emplic.EXPIRYDATE ' +                  'from spectwosuite.employee emp, spectwosuite.license lic, spectwosuite.emplicense emplic'+                  'where emplic.internalstatus<>70 and emplic.internalstatus<>0 ' +                  'and emp.id = emplic.EMPLOYEEID ' +                  'and lic.id = emplic.EMPLICENSETYPEID ' +                  'order by emp.id, lic.id'                  );  Open;  While not Eof do begin    If LastEmpID <> FieldByName('empid').AsInteger then begin      LastEmpID := FieldByName('empid').AsInteger;      inc(CurrentRow);      MyWorksheet.WriteText('.... employee name, surname, id, position, ...');    end;     // find column where to write EXPIRYDATE     For i := 0 to cert_count - 1 do      If ... then        MyWorksheet.WriteText('... EXPIRYDATE  ...');  end;  // write excel to fileend;

Thaddy:
The loop should be handled by query (which uses masks) not as a loop, I explained that. Looping is a great way to show native code is almost always much slower than an SQL database that is properly written. ;D (and that is not a joke but fact)

Adapt your query.

GetMem:
@noszone
When retrieving a lot of records with TSQLQuery, your heap memory may become full, leading to unpredictable behaviour. To solution is simple, set the unidirectional property to true. Please try the following code, before anything else:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---//...SQLQuery5.UniDirectional := True; //add thisSQLQuery5.Open;//SQLQuery5.Last; //this is wrong, not neededSQLQuery5.Firstwhile not SQLQuery5.EOF dobegin  //export here  SQLQuery5.Nextend;

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version