1

I'm working on an ionic2 project which users have to login in first to get access to the system. When they login and its succesfully i then post thier username to a different API to get a list of all entries made by them after which i want to insert all the returned entries into an sql lite db but i'm finding it hard to do

userlogin(){

    let loader = this.LoadingController.create({
      content: 'Please Wait'
      });
      loader.present().then(()=>{

    this.http.post("http://localhost/app/login.php", { 'username': this.username, 'password': this.password }).map(res => res.json()) .subscribe(data => {

      if(data.message!="Incorrect Username or Password"){

     this.http.post("http://localhost/app/entries.php", { 'username': this.username}).map(res => res.json()) .subscribe(data => {
      console.log(JSON.stringify(data));

      this.sqlite.create({
        name: 'entries.db',
        location: 'default'
        })
        .then((db: SQLiteObject) => {
        //data insert section
        db.executeSql('INSERT INTO entries_table(entry) VALUES(?)', 
        [this.data.entry]).then((data)=> {

        }, (error) => {

        });


        })
     });



    }else{

    if(data.message=="Incorrect Username or Password"){
      let alert = this.alertCtrl.create({
        title: 'Error!',
        subTitle: 'Wrong Username or Password',
        buttons: ['Try Again']
      });
      alert.present();
    }
    }
    loader.dismiss();
    });

  },error=>{
    let alert = this.alertCtrl.create({
      title: 'Error!',
      subTitle: 'Please check your Internet Connectivity',
      buttons: ['Try Again']
    });
    alert.present();
  }) 
  }

Logging In works fine but the issue is inserting the multiple data returned by the API at a time into the sql lite db

1 Answer 1

1

Use ionic storage and configure the driver to use sqlite. then it's simple

import { Storage } from '@ionic/storage';

export class MyApp {
  constructor(private storage: Storage) { }

  ...

  // set a key/value
  storage.set('entries', returnedEntryObject);

  // Or to get a key/value pair
  storage.get('entires')
    .then((returnedEntryObject) => {
        console.log('The user entries are', returnedEtnryObject);
  });
}

You can check the details indeep on this link.

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

Comments

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.