sqlite - Calling SQL in JavaScript for loop -


i writing set of functions in javascript user can either add new trip or edit existing trip. each trip can have (0-n) guns associated trip. either way, save , edit function both call rangesavegun. when called “save new trip” function, function (below) executes perfectly. new record added db , displayed. however, when called “edit trip” function, sql same as shown in alert(sql), record never saves db. idea why? in both scenarios, called add new guns trip (not edit existing guns).

my question: in below function, why row not being inserted db when exact same function called “edit” function instead of “new” function? either way “sql” text exactly same (as verified in “alert”). missing? both "new" , "edit" functions called via button click, , save or edit trip , call rangesavegun function @ end.

i using standard workaround ensure closure. can confirm 3 variables (tripnum, input2temp, , input3temp) showing expected values in scenarios. values numbers.

function rangesavegun(tripnum) {  var tbl2 = document.getelementbyid("mytable2");          var rowcount=tbl2.rows.length;  (var = 1; < rowcount; i++) {        var el = document.getelementbyid("gunval"+i)     if(el != null)    {      (function(i){         var input2temp = document.getelementbyid("gunval"+i).value;         var input3temp = document.getelementbyid("rounds"+i).value;         var sql = 'insert rangeshot (tripid, gunid, rounds) values ('+tripnum+', '+input2temp+', '+input3temp+')';         db.transaction(function (tx) {tx.executesql(sql, [])});              alert(sql);       })(i);    }  } } 

i found problem. due me calling insert statement still running while update statement running. db locked first write statement, , second write statement never executed. had re-write "update" function call items in different order.

one other thing ended changing change buttontype "submit" "button".

function rangeupdaterecord()  {  db.transaction(function (tx) {      var input1temp = document.getelementbyid("name").value;     var input2temp = document.getelementbyid("address").value;             var input3temp = document.getelementbyid("lat").value;     var input4temp = document.getelementbyid("lng").value;     var input5temp = document.getelementbyid("duration").value;     var input6temp = document.getelementbyid("date2").value;     var input7temp = document.getelementbyid("starttime").value;     var input8temp = document.getelementbyid("notes").value;         var idupdate = $("#id").val();       var tbl2 = document.getelementbyid("mytable2");          var rowcount=tbl2.rows.length;     (var = 1; < rowcount; i++) {            var el = document.getelementbyid("gunval"+i);           if(el != null)            {               (function(i){                         var input2temp = document.getelementbyid("gunval"+i).value;             var input3temp = document.getelementbyid("rounds"+i).value;             var sql = 'insert rangeshot (tripid, gunid, rounds) values ('+idupdate+', '+input2temp+', '+input3temp+');';             db.transaction(function (tx) {tx.executesql(sql)});                          //alert(sql);             })(i);           }      }          db.transaction(function (tx) {         tx.executesql('update rangetrip set name = ?, address = ?, lat = ?, lng = ?, duration = ?, date2 = ?, starttime = ?, notes = ? id=?', [input1temp, input2temp, input3temp, input4temp, input5temp, input6temp, input7temp, input8temp, number(idupdate)],rangeloadandreset(),onerror);         });      }); 

}


Comments

Popular posts from this blog

c++ - Delete matches in OpenCV (Keypoints and descriptors) -

java - Could not locate OpenAL library -

sorting - opencl Bitonic sort with 64 bits keys -