我对将大量原始数据插入数据库的最佳方式有疑问。
原始数据看起来像这样(它还有很多,但我认为我不需要为我的例子使用所有内容):
CUSACODE - GAME - CONSOLE - REGION - DOWNLOADURL
CUSA00475 Akiba's Trip 2 PS4 JP http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00475_00/4/f_28d6a38b6ebac96d17a8b4de7616f6e5de43da365ef30d8354fb55df94eb0a4a/f/JP0048-CUSA00475_00-AKIBASNEXTAPP000.pkg
CUSA00363 Alien: Isolation - 0 PS4 US http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_0.pkg
CUSA00363 Alien: Isolation - 1 PS4 US http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_1.pkg
CUSA00363 Alien: Isolation - 2 PS4 US http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_2.pkg
使用空格分隔。
所以我认为首先制作一个阵列是一个好习惯。一旦它进入数组,就通过while
和while
SQL INSERT查询循环。
或者为此进行SQL查询会更好吗?有没有办法生成这个SQL查询,所以我不必手动添加所有内容?
我试过这样做:
$data = "CUSA00475|Akiba's Trip 2|PS4|JP|http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00475_00/4/f_28d6a38b6ebac96d17a8b4de7616f6e5de43da365ef30d8354fb55df94eb0a4a/f/JP0048-CUSA00475_00-AKIBASNEXTAPP000.pkg,
CUSA00363|Alien: Isolation - 0|PS4|US|http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_0.pkg,
CUSA00363|Alien: Isolation - 1|PS4|US|http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_1.pkg,
CUSA00363|Alien: Isolation - 2|PS4|US|http://gs2.ww.prod.dl.playstation.net/gs2/appkgo/prod/CUSA00363_00/3/f_e0ebc06845e6271392d9e3060bed29f7a1fb71bc4d8a966714ddc6df50934638/f/UP0177-CUSA00363_00-ALIENISOLATIONP4_2.pkg";
$result = explode("|", $data);
foreach($result as $res) {
$sub = explode(",",$res);
$mainCategory[$sub[0]] = explode("|",$sub[1]);
}
但那非常失败。我希望有人可以帮我解决这个问题!
答案 0 :(得分:1)
如果涉及大量数据,一个好主意是使用准备好的语句,在循环之前初始化。然后在循环内部绑定当前数据并执行语句。
这看起来像这样:
$sql = "INSERT INTO tableName(CUSACODE, GAME, CONSOLE, REGION, DOWNLOADURL) VALUES (?, ?, ?, ?, ?)";
$pstmt = $connection->prepare($sql);
foreach($allData as $data){
$pstmt->bind_param("sssss", $data[0], $data[1], $data[2], $data[3], $data[4]);
$pstmt->execute();
}
$pstmt->close();
根据数据的存储方式,有关如何对数据进行循环的方法有多种。
例如,如果您的数据在某处保存为文本文件,则可以使用以下内容:
$handle = fopen("inputfile.txt", "r");
if ($handle) {
$sql = "INSERT INTO tableName(CUSACODE, GAME, CONSOLE, REGION, DOWNLOADURL) VALUES (?, ?, ?, ?, ?)";
$pstmt = $connection->prepare($sql);
while (($line = fgets($handle)) !== false) {
$data = explode("\t", $line); //if the original delimiter is a tab, which was guessed, because only "whietspace" would make no sense
$pstmt->bind_param("sssss", $data[0], $data[1], $data[2], $data[3], $data[4]);
$pstmt->execute();
}
fclose($handle);
$pstmt->close();
}
编辑:让数据内存已经转换为|
和,
格式,您肯定会首先对explode(',', $data)
进行循环,然后展开|
,如下所示:
$sql = "INSERT INTO tableName(CUSACODE, GAME, CONSOLE, REGION, DOWNLOADURL) VALUES (?, ?, ?, ?, ?)";
$pstmt = $connection->prepare($sql);
$seperatedData = explode(',', $data);
foreach($seperatedData as $oneDataSet){
$seperatedOneData = explode('|', oneDataSet);
$pstmt->bind_param("sssss", $seperatedOneData[0], $seperatedOneData[1], $seperatedOneData[2], $seperatedOneData[3], $seperatedOneData[4]);
$pstmt->execute();
}
$pstmt->close();