Inserting the record into the SQLite table is very easy.
Some times when we try to dynamically insert the record(more than 100 records) into the table will cause some problems.
That time we can able to lose some records in database.
Then how can we avoid this problem?
Yes..It's also very easy., we can use the recursive functions. This function will take care of the problem to insert the record into the SQLite table.
Now we can assume that the data from the service response is as follows,
The above response will looks like JSON format. So we can conver the array format as,
The recursive function of inserting the record into the table are as follows,
Now you will insert all records into the table without losing the data.
Thank you....
Some times when we try to dynamically insert the record(more than 100 records) into the table will cause some problems.
That time we can able to lose some records in database.
Then how can we avoid this problem?
Yes..It's also very easy., we can use the recursive functions. This function will take care of the problem to insert the record into the SQLite table.
Now we can assume that the data from the service response is as follows,
var response =
[
{ "stateId":
1, "stateName": "TamilNadu" },
{ "stateId":
2, "stateName": "Kerala" },
{ "stateId":
3, "stateName": "Karnataka" },
{ "stateId":
4, "stateName": "AndraPradesh" },
{ "stateId":
5, "stateName": "Maharastra" },
{ "stateId":
6, "stateName": "Orissa" },
{ "stateId":
7, "stateName": "MadyaPradesh" },
{ "stateId":
8, "stateName": "WestBengal" },
{ "stateId":
9, "stateName": "Assam"
},
{ "stateId":
10, "stateName": "Gujarat"
},
{ "stateId":
11, "stateName": "JammuKashmir" }
]
The above response will looks like JSON format. So we can conver the array format as,
var myObject =
jQuery.parseJSON(response);
myObject
= eval(myObject);
fnStateInsert(myObject,
0);
The recursive function of inserting the record into the table are as follows,
function
fnStateInsert(serviceResponse, i) {
console.log("Service
Length : " + serviceResponse.length + "
i : " + i);
var
dbinsert = window.openDatabase("IVoteDB",
"1.0", "IVoteDB",
1000000);
if (i <
serviceResponse.length) {
var
dbinsert = window.openDatabase("IVoteDB",
"1.0", "IVoteDB",
1000000);
insertQuery = "INSERT
INTO STATEMASTER(STATEID,STATENAME) VALUES (" + "'" + serviceResponse[i].stateId + "','" + serviceResponse[i].stateName + "')";
dbinsert.transaction(function insertUserPrefDB(tx) {
tx.executeSql(insertQuery);
}, errorCB, function
successCB() {
/* Recursive
Function For Inserting the State in Local Db */
fnStateInsert(serviceResponse, (i +
1));
});
}
else {
alert('State Inserted Successfully');
}
}
Now you will insert all records into the table without losing the data.
Thank you....
can't we insert without creating json? e.g from the datas returned from form tag
ReplyDeleteYes. We can do.
ReplyDeleteThanks it helps me.. :)
ReplyDelete