SQLite in PhoneGap iPhone
I'm trying to use SQLite in phonegap for iPhone, I'm facing the following problem, Please have a look at the below javascript code. Thanks a lot
function testAsSeperateFunctionDB(contact)
{
database.transaction(
function( transaction ){
alert("enteringIntoTransaction");
transaction.executeSql(
'INSERT INTO testTableThree (contactId, mrMrs, firstName,
lastName, jobTitle, bActive, homePhone, contactType, accountId,
workPhone, cellularPhone, workFax, contactEmail, owner,
alternateCountry, alternateAddress1, alternateAddress2,
alternateZipCode, alternateCity, alternateProvince)
VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?);',
[contact.contactId, contact.mrMrs,
contact.firstName, contact.lastName,
contact.jobTitle, contact.bActive,
contact.homePhone, contact.contactType,
contact.accountId, contact.workPhone,
contact.cellularPhone, contact.workFax,
contact.contactEmail, contact.owner,
contact.alternateCountry, contact.alternateAddress1,
contact.alternateAddress2, contact.alternateZipCode,
contact.alternateCity, contact.alternateProvince],
//Callback after successful transaction
function( transaction, results ) {
alert("added");
},
//Callback after error in transaction
function( transaction, error ) {
alert("Error" + error.code);
}
);
}
);
}
I'm using the above function to add a set of contact details into a table, I wil开发者_开发问答l be passing a JSON object to this function. I'm calling the above function in the following loop structure.
for(i = 0; i < contactFullObjArray.length; i++) {
//Parse the ContactFull object
var contact=ContactFull.parse(contactFullObjArray[i]);
testAsSeperateFunctionDB(contact);
}
What's happening here is transaction is happening only after the whole for loop is executed, and it is getting called only once, which makes only the final values that are passed to be added into the table. (I'm getting the alert "enteringIntoTransaction" only once, and that value is getting added into the database. Thanks a lot for your help
I solved this by placing the
transaction.executeSql()
inside the for loop and iterated through it.
精彩评论