Sajid saiyed,

 

The problem is actually not due to the speed of the database (though that
can be dramatically increased by doing all the INSERTs in a single
transaction).

 

The problem is due to a common "gotcha" - referring to a loop variables in a
closure. You can read all about it on developer.mozilla.org's Closures
<https://developer.mozilla.org/en/JavaScript/Guide/Closures>  page -
specifically the "Creating closures in loops: A common mistake" section.

 

Of course their first solution - the "let" keyword introduced in Mozilla's
Javascript 1.7 - isn't available in Safari Mobile. So you'll have to use
their second solution - which is to use more closures in the form of a
function factory. You can do the same thing concisely by using the Prototype
library <http://www.prototypejs.org/api/function/curry> 's curry() function,
like this:

 

addG: function(fname, fid){

       alert('4'+fid);

       for(var i=0; i<95; i++){

              alert('5');

              this.dbConn.transaction(function (fid, transaction) {

                     transaction.executeSql('INSERT INTO `GTable`
(`id`,`gname`)  VALUES (?, ?);', [fid, i],
classObj.nullDataHandler,classObj.errorHandler);

              }.curry(fid));

       }

}

 

If you're not already including the Prototype library (and you're probably
not), you can include just their curry function, like so:

  Function.prototype.curry = function() {

    if (!arguments.length) return this;

    var __method = this, args = Array.prototype.slice.call(arguments, 0);

    return function() {

      var a = merge(args, arguments);

      return __method.apply(this, a);

    }

  }

 

Of course if you're against modifying the built-in Function.prototype (as
most JS developers are these days), you can revise the above to be a
function that takes in a function.

 

To make it orders of magnitude faster, do it all in a single transaction,
like this:

 

addG: function(fname, fid){

       alert('4'+fid);

              this.dbConn.transaction(function (fid, transaction) {

       for(var i=0; i<95; i++){

              alert('5');

                     transaction.executeSql('INSERT INTO `GTable`
(`id`,`gname`)  VALUES (?, ?);', [fid, i],
classObj.nullDataHandler,classObj.errorHandler);

       }

              }.curry(fid));

}

 

 

-- peter rust

Software Development Manager, Cornerstone Systems

 

From: [email protected] [mailto:[email protected]]
On Behalf Of sajid saiyed
Sent: Monday, August 02, 2010 7:27 PM
To: [email protected]
Subject: Inserting multiple rows in Sqlite database

 

Hi,

I am making an iPad app which uses localstorage Sqlite database.

 

I am having some problems with the performance of the database queries.

 

Here is my code:

 

addG: function(fname, fid){

            alert('4'+fid);

            for(var i=0; i<95; i++){

                        alert('5');

                        this.dbConn.transaction(function (transaction) {

                                    transaction.executeSql('INSERT INTO
`GTable` (`id`,`gname`)  VALUES (?, ?);', [fid, i],
classObj.nullDataHandler,classObj.errorHandler);

                        });

            }

}

 

What actually happens that the insert query works so slow that after a first
few records, all entries insert a value of 95 in the 'gname' column.

 

Is there an optimised way of inserting multiple entries in sqlite database?

 

Thanks

-- 
You received this message because you are subscribed to the Google Groups
"iPhoneWebDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/iphonewebdev?hl=en.

-- 
You received this message because you are subscribed to the Google Groups 
"iPhoneWebDev" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/iphonewebdev?hl=en.

Reply via email to