我有一个代码将xlsx上传到Mysql表,并且加载是用块完成的,因为文件太大,所以我必须插入每个块。 for循环运行良好,已经使用echo语句进行了测试,但INSERT仅在第一个块中完成。知道怎么解决这个问题吗?这是代码的一部分:
for ($startRow = 2; $startRow <= $totalRows; $startRow += $chunkSize) {
$chunkFilter->setRows($startRow,$chunkSize);
//Load of the data
$objPHPExcel = $objReader->load($newFilePath);
$objWorksheet4 = $objPHPExcel -> getSheetByName("MIFReportExport");
//Getting data of every cell
$highestRow4 = $objWorksheet4->getHighestRow();
$highestColumn4 = $objWorksheet4->getHighestColumn();
$highestColumnIndex4 = PHPExcel_Cell::columnIndexFromString($highestColumn4);
$rows = array();
for ($row = 2; $row <= $highestRow4; ++$row) {
for ($col = 0; $col <= $highestColumnIndex4; ++$col) {
$rows[$col] = $objWorksheet4->getCellByColumnAndRow($col, $row)->getCalculatedValue();
}
$result = mysql_query("INSERT INTO fy16 (SerialNumber, BrandedModelName, GenericProductCode, Familia, ProductLineModelFile)VALUES ('".$rows[0]."', '".$rows[1]."', '".$rows[2]."', '".$rows[3]."', '".$rows[4]."', '".$rows[5]."')",$con) or die(mysql_error());
}
}
提前致谢。
答案 0 :(得分:1)
首先,你应该看一下PDO和准备好的陈述。 以下是如何创建MASS插入查询
#!/usr/bin/perl -w
use utf8;
use strict;
use feature qw(:5.10);
use Encode qw(encode is_utf8);
use DBI;
use Data::Dumper;
my $str = 'æøå';
my $latin1 = encode('latin1', $str);
my $utf8upgraded = $latin1;
utf8::upgrade($utf8upgraded);
# $str, $latin1 and $utf8upgraded are all eq each other:
$str eq $latin1
or die;
$str eq $utf8upgraded
or die;
$latin1 eq $utf8upgraded
or die;
my $dbh = DBI->connect(
"DBI:mysql:mysql",
'user',
'pass',
);
$dbh->do(q(
CREATE TEMPORARY TABLE test (
name VARCHAR(255) DEFAULT NULL
) CHARSET=latin1;
));
$dbh->do(q(
INSERT INTO test (name) VALUES ('');
));
sub mysql_roundtrip_convert {
my ($val) = @_;
my $sth = $dbh->prepare('SELECT CONVERT(? USING LATIN1)');
$sth->execute($val);
my ($concat) = $sth->fetchrow_array();
return $concat;
}
sub mysql_roundtrip_column {
my ($val) = @_;
my $updateSth = $dbh->prepare('update test set name=?');
$updateSth->execute($val);
my $getSth = $dbh->prepare('select name from test');
$getSth->execute();
my ($value) = $getSth->fetchrow_array();
return $value;
};
sub mysql_roundtrip {
my ($val) = @_;
# Check that these two are the identical:
my $column = mysql_roundtrip_column($val);
my $convert = mysql_roundtrip_convert($val);
$column eq $convert
or die "column ne convert";
return $column;
}
sub mysql_value_latin1 {
my ($val) = @_;
# See text - this looks strange - but works!
if (is_utf8($val)) {
$val = encode('utf8', $val);
} else {
$val = encode('latin1', $val);
}
return $val;
}
sub new_value_will_change_database {
my ($current_value, $new_value) = @_;
my $mysql_new_value = mysql_value_latin1($new_value);
return $current_value ne $mysql_new_value;
}
my @tests = (
[ 'str', $str ],
[ 'latin', $latin1 ],
[ 'utf8upgraded', $utf8upgraded ],
map { [ 'char ' . $_ , 'char' . chr($_) ] } ( 0x00 .. 0xFF ),
);
foreach (@tests) {
my ($disp, $val) = @$_;
my $mysql_roundtrip = mysql_roundtrip($val),
my $mysql_value_latin1 = mysql_value_latin1($val);
$mysql_value_latin1 eq $mysql_roundtrip
or die "mysql_value_latin1 ne mysql_roundtrip";
}
print "All test are fine\n";