Saturday, June 9, 2018

Sqlite in ionic 2 / ionic 3 / ionic 4, how to use it?

Ionic 2 is today one of the most popular open source and free hybrid mobile framework for build hybrid mobile apps for Android ,iOS and even Windows Universal Platform .
Installation :-
  1. Install the Cordova and Ionic Native plugins:
  2. $ ionic cordova plugin add cordova-sqlite-storage
  3. $ npm install –save @ionic-native/sqlite

import sqlite from ionic-native :
import { SQLite, SQLiteObject } from ‘@ionic-native/sqlite’;
Use "private sqlite: SQLite" in constructor :
constructor(private sqlite: SQLite) {
  this.sqlite.create({
     name: ‘data.db’,
     location: ‘default’
   })
   .then((db: SQLiteObject) => {
      db.executeSql(‘create table user(name VARCHAR(32))’, {})
.then(() => console.log(‘Executed SQL’))
      .catch(e => console.log(e));
})
.catch(e => console.log(e));
}

No comments:

Post a Comment