0

Currently am inserting one single value into db using SQL in javascript (Phonegap application)

the following code am using to insert a value into db.

But when i tried to run that INSERT query in a loop it showing memory issues like 2.05GB memory used on run time

    function populateDB(tx) {

    var newsArray=new Array();
    newsArray[0]="one";
    newsArray[1]="two";
    newsArray[2]="three";
    newsArray[3]="four";
    newsArray[4]="five";
     tx.executeSql('DROP TABLE IF EXISTS NEWS');
 tx.executeSql('CREATE TABLE IF NOT EXISTS NEWS (id PRIMARY KEY AUTO INCREMENT ,news TEXT NOT NULL)');
var i=0;
while(i<newsArray.length){


 tx.executeSql('INSERT INTO NEWS (news) VALUES ('newsArray[i]')');
    i++;
}

                //Dropping and adding makes sure there is one entry of the news item.
                //You may actually cache multiple copies and make it a feature as well.
}
Bangalore
  • 1,572
  • 4
  • 20
  • 50

1 Answers1

1

Check this answer https://stackoverflow.com/a/5009740/1576274 for how to insert multiple rows with one statement in SQLITE. That could give you a big performance bump over the looping insert.

Also the drop and create table might be expensive - is there a way to uniquely identify your source data so that you only have to insert if it doesn't exist?

Community
  • 1
  • 1
morefromalan
  • 302
  • 3
  • 9