Skip to content Skip to sidebar Skip to footer

Unable To Create Sqlite Tables Using Javascript In For Loop

I have a cross platform app developed using AngularJS, JS, Phonegap/Cordova, Monaca and Onsen UI. I have implemented a SQLite Database in the app to store various data to ensure th

Solution 1:

I would use the JavaScript "Object" aproach to avoid conflict when calling function 'createFormTables' with something like:

varQuerySet = function(){
//OPTIONAL: I use JQuery Deferred to catch when an async event has finishedthis.defRes = new $.Deferred();
    this.defResProm = this.defRes.promise();
};

QuerySet.prototype.createFormTables= function(inputData){
      //do your stuff and manage when this.defRes is resolved and results it sends back (if a result is needed)this.sqlToExecute = "// YOUR SQL QUERY"; 
     var self=this;
    $.when(
        (newSqlResult(this.sqlToExecute)).execSqlCustomDeferred(), //read below for SqlResult() explanations
        self
    ).done(function(sqlRes,self){
        self.defRes.resolve();
    });

      returnthis.defResProm;
};

Then in the code:

creatFromTables[i] = (newQuerySet()).createFormTables(inputData);
createFromTables[i].defRes.done(function(res){//do stuff with the result});

Don't need to use $.Deferred() but I think it could be useful if you want to know when all the tables have been created.

And here is sqlResult that is used to call the transaction on the DB itself:

varSqlResult = function(sqlToExecute,bracketValues){
            //console.log("SqlResult("+sqlToExecute+','+bracketValues+') starts');this.sqlToExecute = sqlToExecute;
    this.bracketValues =bracketValues;
};

SqlResult.prototype.execSqlCustomDeferred = function(){
        var execSqlCustomDeferredRes = $.Deferred();
        var execSqlCustomDeferredResProm = execSqlCustomDeferredRes.promise();  

        //console.log("SqlResult.execSqlCustomDeferred sqlToExecute is: "+this.sqlToExecute);var sqlToExecuteForTx = this.sqlToExecute;
        var bracketValuesForTx = this.bracketValues;

        DbManagement.db.transaction(function(tx){

            console.log("SqlResult.execSqlCustomDeferred in TX sqlToExecute is: "+sqlToExecuteForTx);

            if(bracketValuesForTx!=null){
                console.log("SqlResult.execSqlCustomDeferred bracket value is: "+ArrayManagement.arrayToString(bracketValuesForTx));
            }
            tx.executeSql(sqlToExecuteForTx,bracketValuesForTx,success,error);
            functionsuccess(tx,rs){
                //console.log('before execSqlCustomDeferredRes resolve ' + execSqlCustomDeferredRes.state());
                execSqlCustomDeferredRes.resolve(rs);
                //console.log('before execSqlCustomDeferredRes resolve  after ' + execSqlCustomDeferredRes.state());

            }
            functionerror(tx,error){
                console.log('execSqlCustomDeferred error ' + error.message);
                execSqlCustomDeferredRes.reject(error);
            }

            });

        return execSqlCustomDeferredResProm;
};

Post a Comment for "Unable To Create Sqlite Tables Using Javascript In For Loop"