I'm trying to populate a phonegap sqlite database with a JSON array, the web service to get the JSON is working and the data is loaded just fine but when I try insert it into my table it keeps giving me an undefined sql error.
Here's my code:
function populateDB(tx) {
//Setup database table
tx.executeSql('DROP TABLE IF EXISTS Profiles');
tx.executeSql('CREATE TABLE IF NOT EXISTS Profiles (id unique, name, email, phone)');
//Get data
var values;
var base_url = "https://www.hoomz.nl/staging/index.php/api/";
$.getJSON(base_url + 'profiles', function(result) {
$.each(result, function(i, item) {
console.log(item.name);
console.log('(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '"),');
values = values + '(' + item.id + ', "' + item.id + '", "' + item.id + '", "' + item.id + '"),';
});
console.log(values);
tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES' + values + ';');
});
console.log(values);
tx.executeSql('INSERT INTO Profiles (id, name, email, phone) VALUES' + values + ';');
}
Also gives this error:
Uncaught InvalidStateError: An attempt was made to use an object that is not, or is no longer, usable.