2012-10-26 38 views
0

我正在檢索使用AJAX調用Web服務的服務調用列表。我需要遍歷Web服務返回的每個服務調用並將其插入到SQLite表中。我在循環中苦苦掙扎。例如,如果有兩個服務調用,則兩行將被插入表中,但兩者都將是第二次調用的數據。我嘗試過移動我的變量聲明和值賦值,如果它們在事務中,那麼所有的字段都會返回undefined。在事務之外,所有行都插入最後一條記錄。迭代通過列表插入到SQLite表

$.ajax({ 
    type: "POST", 
    dataType: "json", 
    contentType: "application/json; charset=utf-8", 
    url: "MyService.asmx/GetCalls", 
    data: '{ user:"' + user +'"}', 
    success: function (data) { 
     saveCalls(data.d); 
    }, 
    error: function (xhr) { 
     var err = eval("(" + xhr.responseText + ")"); 
     alert("Retrieve Calls: " + err.Message); 
    } 
}); 

function saveCalls(result) { 
    var insertString = "Insert Into Calls Values(?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)"; 

    var call, po, adrsCode, status, dispDate, dispTime, desc, customer, address, city, state, zip, phone, equipment, tracking, srvType, statDesc; 

    $.each(result, function() { 
     call = this.CallNumber; 
     po = this.PoNumber; 
     adrsCode = this.AdrsCode; 
     status = this.Status; 
     dispDate = this.DispatchedDate; 
     dispTime = this.DispatchedTime; 
     desc = this.Description; 
     customer = this.Customer; 
     address = this.Address; 
     city = this.City; 
     state = this.State; 
     zip = this.Zip; 
     phone = this.Phone; 
     equipment = this.Equipment; 
     tracking = this.Tracking; 
     srvType = this.SrvType; 
     statDesc = this.StatDesc; 
     db.transaction(function (tx) { 

      tx.executeSql(insertString, [call, po, adrsCode, status, dispDate, dispTime, desc, customer, address, 
      city, state, zip, phone, equipment, tracking, srvType, statDesc], 
      onSqlSuccess, onSqlError); 

    }); 
}); 

}

編輯:代碼,對於那些誰有同樣的問題的工作:

function saveCalls(result) { 
    var insertString = "Insert Into Calls(CallNumber, PoNumber, AdrsCode, SrvStat, DispatchDate, DispatchTime, SvcDescription, CustNmbr, Address, " 
      + "City, State, Zip, Phone, Equipment, Tracking, SrvType, StatDesc) "; 

    $.each(result, function() { 

     insertString = insertString + "Select '{0}' as CallNumber,'{1}' as PoNumber,'{2}' as AdrsCode,'{3}' as SrvStat,'{4}' as DispatchDate,'{5}' as DispatchTime,'{6}' as SvcDescription,'{7}' as CustNmbr,'{8}' as Address,'{9}' as City,'{10}' as State,'{11}' as Zip,'{12}' as Phone,'{13}' as Equipment,'{14}' as Tracking,'{15}' as SrvType,'{16}' as StatDesc Union " 
        .format(this.CallNumber, this.PoNumber, this.AdrsCode, this.Status, this.DispatchedDate, this.DispatchedTime, 
         this.Description, this.Customer, this.Address, this.City, this.State, this.Zip, this.Phone, this.Equipment, 
         this.Tracking, this.SrvType, this.StatDesc); 

    }); 

    db.transaction(function(tx) { 
     tx.executeSql(insertString.substring(0, insertString.length - 6), [], 
     onSqlSuccess, onSqlError); 
    }); 

}

+0

找到了答案。根據要求的時間段,儘快回覆標記。 – jmease

回答

0

請參閱添加到原來的職位額外的代碼,任何人與掙扎同樣的問題。