2010-11-04 22 views
0

我有一個像Perl一樣的數組(只有更大)。我的數組被稱爲@sqlInsert如何將數組的一部分插入到MySQL中?

部件1 一個 乙 Ç

元素2 一個 乙 Ç

元素3 一個 乙 Ç

我想填充一個MySQL表h這些數據中的信息。所以真的,我想要有一排:

for ($count=0; $count<@arrayInsert; $count++) { 
     INSERT INTO table values ("sqlInsert[$i]","sqlInsert[$i+1]","sqlInsert[$i+2]","sqlInsert[$i+3]") 
} 

任何指針?

+0

帶有二進制的按鈕意味着「格式爲代碼」。 – Ether 2010-11-04 23:09:48

回答

3

使用DBI placeholders和...

...數組切片:

my $sth = $dbh->prepare('INSERT INTO table VALUES (?, ?, ?, ?)'); 

$sth->execute(@sqlInsert[ $n .. $m ]); 

...或剪接:

while(@sqlInsert) { 
     $sth->execute(splice @sqlInsert, 0, $length,()); 
     } 

這可能是更容易地創建一個數據結構,如陣列陣列,已經將每個結構的元素分組:

foreach my $ref (@sqlInsert) { 
     $sth->execute(@$ref); 
     } 
+0

感謝您的回覆,Brian ......我帶着您的第一個解決方案前往。但是,這裏是錯誤,我得到: >在sqlInsert.pl行56的範圍(或觸發器)中使用未初始化的值。 >在sqlInsert.pl行56的範圍(或觸發器)中使用未初始化的值。 >參數「」在sqlInsert.pl行56的數組切片中不是數字。 > DBD :: mysql :: st執行失敗:在sqlInsert.pl行56需要4時調用1綁定變量。 這裏是我的行56: > $ sth-> execute(@td [$ n .. $ m]); – poutine 2010-11-04 22:41:03

+3

我點了亞馬遜的水晶球。當它到達這裏時,我會查看你的代碼。 :) – 2010-11-04 22:43:28

+0

公平的好友,我可能得到: > #Auth > $ platform =「mysql」; > $ database =「db」; > $ host =「localhost」; > $ user =「root」; >#連接到DB > $ DBIconnect = DBI-> connect($ dsn,$ user); > my $ sth = $ DBIconnect-> prepare('INSERT INTO table VALUES(?,?,?,?)'); > $ sth-> execute(@sqlInsert [$ n .. $ m]); \t ------ 你能讓我知道$ n ... $ m是做什麼的嗎? – poutine 2010-11-04 22:47:10

相關問題