0

I'm creating app with using SQLite. I have some problems with using SQLite.

I have page with variable for managing database.

public sqlite_object: any;

In the constructor i'm opening/creating if not exists database, and saving db object to variable.

constructor(...){
   let name = 'db_name';
   this.openDatabase(name);
}

openDatabase(name){
   let db = new SQLite();
   db.create({ name: name, location: 'default' })
   .then( (db_obj: SQLiteObject) => {
      this.sqlite_object = db_obj
   }, (error) => {
      alert('error');
   }
}

So, in the constructor i'm opening db, and saving it for future. On of buttons calling that function:

testSQL(sql_queries){
   this.sqlite_object.transaction( function(tx){
      Object.keys(sql_queries)
      .sort()
      .forEach( function(v,i){
         tx.executeSql(sql_queries[v],
         null,
         function (transaction, result){
            alert('executing sql');
         },
         function (transaction, error){
            alert('error');
         });
      });
   }, function (error){
      alert('error2');
   }, function (){
      alert('success');
   }
} 

My sql_queries have about ~30 queries (correct and incorrect). When i put alert into forEach(), it will be executed everytime (same times as sql_queries length). When executeSql(...) getting incorrect query, alert about error is showing, but i never seen alert 'executing sql' - is sth wrong here? (i don't know if my queries executing correctly)

Also i have one more question. How can i get list of tables from my database?

1 Answer 1

1

your "executing sql.." is inside a success callback function. it will be executed after the entire for each() query is successful (not after each loop). So as you said it's showing error. So, It will not execute the successcallback function. else it will execute the errorcallback function. this is beacause your entire query is not successful. I hope you get the point

Sign up to request clarification or add additional context in comments.

1 Comment

ohh, ok , i understand. Maybe you know how can i get list of tables from my db?

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.