0

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");
       });
    });
 }
7
  • How many "Success" messages do you see in the console? Commented May 14, 2015 at 8:20
  • 68(json response size) logs @Googie Commented May 14, 2015 at 8:50
  • How was the news_posts table created? Could you please paste the CREATE TABLE statement for it? (you can get it by with SELECT sql FROM sqlite_master WHERE name = 'news_posts';) Commented May 14, 2015 at 9:01
  • I updated the post with that code. @Googie Commented May 14, 2015 at 9:09
  • You should provide also an error handler function (not just result handler), because having call to result handler does not mean that it succeeded. Add one more handler function to your query, which will print errors for you. Then see if you have errors, or not. Commented May 14, 2015 at 10:13

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.