Large number of SQLite inserts using PHP

前端 未结 4 1245
无人共我
无人共我 2021-01-13 01:54

I have about 14000 rows of comma separated values that I am trying to insert into a sqlite table using PHP PDO, like so:



        
4条回答
  •  臣服心动
    2021-01-13 02:22

    You will see a good performance gain by wrapping your inserts in a single transaction. If you don't do this SQLite treats each insert as its own transaction.

    beginTransaction();
    $lines = file('/csv/file.txt'); // import lines as array
    foreach ($lines as $line) {
        $line_array = (','$line); // create an array of comma-separated values in each line
        $values = '';
        foreach ($line_array as $l) {
            $values .= "'$l', ";
        }
        substr($values,-2,0); // get rid of the last comma and whitespace
        $query = "insert into sqlite_table values ($values)"; // plug the value into a query statement
        $dbh->query($query); // run the query
    }
    // commit transaction
    $dbh->commit();
    
    ?>
    

提交回复
热议问题