Hello,
I have a question on how to insert multiple records to sqlite using for loop.
What I would like to do is to insert many record ~10,000 to sqlite. After all records are inserted successfully, to
Set date in localstorage and read the data from the db back.
My problem is with the async nature of the executeSq() API. what happens, is that my read API is called before the insert completes.
This is basically what I am doing:
insertProduct(products) {
let i: number = 0;
for (let product of products) {
this.database.executeSql('INSERT INTO ' + this.TABLE_NAME + ' (product) VALUES(?);', JSON.stringify(product)).then(res => {
i++;
if (i == products.length) {
this.setLastUpdatedDate();
}
}, err => {
this.presentToast('sqdb_insertProduct fail: ' + err);
});
}
}
How do I call this API so when all data is inserted reading back?
This is my read API:
getAllProducts() {
return this.database.executeSql('SELECT * FROM ' + this.TABLE_NAME + ';', []).then(data => {
let products = [];
for (var i = 0; i < data.rows.length; i++) {
products.push(data.rows.item(i).product);
}
return products;
}, err => {
this.presentToast('sqdb_getAllProducts fail: ' + err);
return [];
});
}
My second question, is how to read back all data from the db and once it returns to continue doing other stuff?
Thanks,