I have the following code to insert JSON object into SQLITE database. I used for loop to insert each json object data into the database. But the last JSON object is only inserted into the database.
db.transaction(function(tx) {
tx.executeSql('CREATE TABLE news_posts (id integer primary key, title text, content text,thumbnail text,url text,date datetime)');
});
for (var i = 0; i < json.posts.length; i++) {
db = window.openDatabase("news", "1.0", "Posts database", 200000);
db.transaction(function(tx) {
tx.executeSql("INSERT INTO news_posts (title,content,thumbnail,url,date) VALUES (?,?,?,?,?)", [json.posts[i].title,json.posts[i].content,json.posts[i].thumbnail,json.posts[i].url,json.posts[i].date], function(tx, res) {
console.log("Success");
});
});
}
news_poststable created? Could you please paste theCREATE TABLEstatement for it? (you can get it by withSELECT sql FROM sqlite_master WHERE name = 'news_posts';)