Note that there are some explanatory texts on larger screens.

plurals
  1. POphp fgetcsv multiple lines not only one or all
    primarykey
    data
    text
    <p>I wand to read biiiiig CSV-Files and want to insert them into a database. That already works:</p> <pre><code>if(($handleF = fopen($path."\\".$file, 'r')) !== false){ $i = 1; // loop through the file line-by-line while(($dataRow = fgetcsv($handleF,0,";")) !== false) { // Only start at the startRow, otherwise skip the row. if($i &gt;= $startRow){ // Check if to use headers if($lookAtHeaders == 1 &amp;&amp; $i == $startRow){ $this-&gt;createUberschriften( array_map(array($this, "convert"), $dataRow ) ); } else { $dataRow = array_map(array($this, "convert"), $dataRow ); $data = $this-&gt;changeMapping($dataRow, $startCol); $this-&gt;executeInsert($data, $tableFields); } unset($dataRow); } $i++; } fclose($handleF); } </code></pre> <p>My problem of this solution is, that it's very slow. But the files are too big to put it directly into the memory... So I wand to ask, if there a posibility to read, for example 10 lines, into the $dataRow array not only one or all. I want to get a better balance between the memory and the performance.</p> <p>Do you understand what i mean? Thanks for help.</p> <p>Greetz</p> <p>V</p> <p><strong>EDIT:</strong> Ok, I still have to try to find a solution with the MSSQL-Database. My solution was to stack the data and than make a multiple-MSSQL-Insert:</p> <pre><code> while(($dataRow = fgetcsv($handleF,0,";")) !== false) { // Only start at the startRow, otherwise skip the row. if($i &gt;= $startRow){ // Check if to use headers if($lookAtHeaders == 1 &amp;&amp; $i == $startRow){ $this-&gt;createUberschriften( array_map(array($this, "convert"), $dataRow ) ); } else { $dataRow = array_map(array($this, "convert"), $dataRow ); $data = $this-&gt;changeMapping($dataRow, $startCol); $this-&gt;setCurrentRow($i); if(count($dataStack) &gt; 210){ array_push($dataStack, $data); #echo '&lt;pre&gt;', print_r($dataStack), '&lt;/pre&gt;'; $this-&gt;executeInsert($dataStack, $tableFields, true); // reset the stack unset($dataStack); $dataStack = array(); } else { array_push($dataStack, $data); } unset($data); } $i++; unset($dataRow); } } </code></pre> <p>Finaly I have to loop the Stack and build in mulitiple Insert in the method "executeInsert", to create a query like this:</p> <pre><code>INSERT INTO [myTable] (field1, field2) VALUES ('data1', 'data2'),('data2', 'datta3')... </code></pre> <p>That works much better. I still have to check the best balance, but therefor i can change only the value '210' in the code above. I hope that help's everybody with a similar problem. Attention: Don't forget to execute the method "executeInsert" again after readin the complete file, because it could happen that there are still some data in the stack and the method will only be executed when the stack reach the size of 210....</p> <p>Greetz</p> <p>V</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload