兼容的Perl格式數組元素想做與$ ALL_Total_Attach_ENB_data以下[0]爲MySQL插入
- 選擇從存儲在$ ALL_Total_Attach_ENB_data [0]的CSV數據的特定字符串。
- 用單引號括起它們
- 將該數據作爲輸入傳遞給mysql插入到語句中。
我已經完成了Step-1並且不知道如何有效地執行Step-2步驟2 & 3.可能有一個襯墊可以提供幫助。
實際數據 $ ALL_Total_Attach_ENB_data [0] =」,ITEM,=,ENB,INTERVAL,=,HOUR,DURATION,=,2013-10-16,11:00,〜,2013年10月16日,12:00,ENB,TYPE,嘗試,ACCEPT,COMB_ACPT,CTXT_RSP,SUCCESS,HSS_FAIL,ERR_IMEI,ERR_MS,ERR_SVC,GW_FAIL,GW_TO,ENB_FAIL,ENB_TO,UE_FAIL,ESM_FAIL,SM_FAIL,SM_TO,S1_REL,RSC_FULL,SYS_FLT,HSS_TO ,AUC_FAIL,AUC_TO,EIR_FAIL,EIR_TO,MS_TO,科利斯,UNK_PLMN,APN_FAIL,ODB_BAR,DNS_FAIL,DNS_TO,CMP_TO,S1_REL1,ERR_ETC,CANCEL,DTCH,S_RATIO,C_RATIO,SET_TIME,AVG_CPS,PEAK_CPS,SS_RATIO,TOTAL,INIT,162797 ,150716,0,150134,149450,0,1,0,0,4458,0,10,0,525,245,0,0,39,0,1,0,51,0,0,0,314,18,0,5950 ,0,1025,0,605,2,2,0,80,92.58,91.80,583.20,45,48,97.25,梳子,17219,4601,0,4571,4560,0,0,0,0,1075,0 ,0,0,25,27,7,0,2,0,0,0,243,0,0,0,103,1,300,10691,0,163,0,7,0,0,0,12,26.72,26.48,584.81 ,5,6,93.72,肌電圖,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0.00,0.00,0.00,0,0,0.00" ;
my @tmp = split(',', $ALL_Total_Attach_ENB_data[0]);
$val1=join(',' , @tmp[9,10,12,13,60 .. 103]);
$db ="4ghourly";
$user = "root";
$pass = "root";
$host="localhost";
$port="3307";
$dbh = DBI->connect("DBI:mysql:$db:$host:$port", $user, $pass);
$query = "INSERT INTO attach (startDate,startTime,endDate,endTime,type,attempt,accept,comb_acpt,ctxt_rsp,success,hss_fail,err_imei,err_ms,err_svc,gw_fail,gw_to,enb_fail,enb_to,ue_fail,esm_fail,sm_fail,sm_to,s1_rel,rsc_full,sys_flt,hss_to,auc_fail,auc_to,eir_fail,eir_to,ms_to,collis,unk_plmn,apn_fail,odb_bar,dns_fail,dns_to,cmp_to,s1_rel1,err_etc,cancel,dtch,s_ratio,c_ratio,set_time,avg_cps,peak_cps,ss_ratio) VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
$sqlQuery = $dbh->prepare($query) or die "Can't prepare $query: $dbh->errstr\n";
$rv = $sqlQuery->execute('2013-10-30','11:00','2013-10-30','12:00','INIT','162797','150716','0','150134','149450','0','1','0','0','4458','0','10','0','525','245','0','0','39','0','1','0','51','0','0','0','314','18','0','5950','0','1025','0','605','2','2','0','80','92.58','91.8','583.2','45','48','97.25') or die "can't execute the query: $sqlQuery->errstr";
如果你在@tmp中有正確的數據,你可以執行'$ rv = $ sqlQuery-> execute(@tmp);'。你可以看看拼接設置你的陣列是否正確或可能http://search.cpan.org/~makamaka/Text-CSV-1.32/lib/Text/CSV.pm更好的解決方案處理引用數據包含逗號 – KeepCalmAndCarryOn
考慮添加[RaiseError](http://search.cpan.org/~timb/DBI-1.628/DBI.pm#RaiseError)到你的connect() - 它會照顧'...或死... '邏輯給你。 – pilcrow