Many SQL-Statements


#1

Hey guys,
I need to insert around 4000 rows into my apps sqlite database. I’ve tried a lot of things, but
most of them not work properly.
Does someone has a good working solution?

I know that this isn’t the right place for my question, but I hope that someone of you may help me.

Thanks for your responses!


#2

Ok. I found the following solution :wink:

self.querys = function(querys) 
    //start insertation
    var ready_count = 0;
 
    querys.forEach(function(statement,index){
        self.db.transaction(function(transaction) {
            transaction.executeSql(statement, null, function(transaction, result) {
                ready_count ++;
                if (ready_count >= querys.length)
                    deferred.resolve("fertig");
            }, function(transaction, error) {
                if (ready_count >= querys.length)
                    deferred.resolve("fertig");
            });
        });
    });
    return deferred.promise;
};

#3

i would use the $q.all method to know, if all promises are ready.

the idea is to simplify your code -> move your loop-content to an own function.
Fill your task array in the loop with function calls.

call $q.all on that array to get notified if everything works, or something failes.

something like that:

function makeTransaction(statement, transaction) {
  var deferred = $q.defer();

 self.db.transaction(function(transaction) {
        transaction.executeSql(statement, null, function(transaction, result) {
            deferred.resolve("fertig");
        }, function(transaction, error) {
            deferred.reject('error');
        });
  });

 return deferred.promise;
}

self.querys = function(querys) 
  //start insertation
  var ready_count = 0,
       queryTasks = [];

  querys.forEach(function(statement,index){
    self.db.transaction(function(transaction) {
      queryTasks(makeTransaction(statement, transaction));    
    });
  });

  $q.all(queryTasks).then(function () {
    deferred.resolve();
  }, function () {
    deferred.reject();
  });
  return deferred.promise;
};