user6579134
user6579134

Reputation: 839

Insert multiple data return from http API into sql lite

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

Upvotes: 1

Views: 37

Answers (1)

Jean Guzman
Jean Guzman

Reputation: 2202

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.

Upvotes: 1

Related Questions