【发布时间】:2011-05-09 13:23:26
【问题描述】:
我尝试使用 PHP PDO 将大约 14000 行逗号分隔值插入到 sqlite 表中,如下所示:
<?php
// create a PDO object
$dbh = new PDO('sqlite:mydb.sdb');
$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
}
?>
这个查询需要很长时间,并且要在不中断的情况下运行它,我必须使用 PHP-CLI。 有没有更好(更快)的方法来做到这一点?
【问题讨论】: