facebook

Why use Ionic SQLite instead of Local Storage to build an App?

By Soumik Hens

Why use Ionic SQLite instead of Local Storage to build an App?

First of all, SQLite is not an SQL database. It is an embedded SQL database engine. SQLite does not have any separate server process like the other SQL databases have. SQL database which includes tables, views and other things are contained in a single disk file whereas SQLite operates by directly reading and writing to ordinary files on the disk.

SQLite implements serverless, self-contained, transactional, zero-configuration SQL database engine which is an in-process library. SQLite is free and is in public domain also, so it can be used for any private and commercial purpose.

Ionic Local Storage uses the local storage system of browser for key/value pairs storing. The limit of Local Storage is only 5MB. It is heard that an issue with local storage is that if iOS is out of memory, it can delete local storage data to gain memory space. Local Storage should be used only for temporary data storing where data loss can be affordable. Local Storage must be cleaned up by the operating system(iOS) to manage the storing of data under 5MB. If more space is needed for storing data, then SQLite is more preferable which uses SqlStorage engine.

SQLite is a widely used file-based database and one of the most stable storage system for Ionic App.

 

For using the SOLite, first we need to install the cordova-sqlite-storage plugin:

ionic cordova plugin add cordova-sqlite-storage

 

After that, we need to install the package

npm install –save @ionic/storage

 

Then, import it in the app.module.ts and add it to the imports

import { IonicStorageModule } from ‘@ionic/storage’;

@NgModule({

  declarations: [

    // …

  ],

  imports: [

    BrowserModule,

    IonicModule.forRoot(MyApp),

    IonicStorageModule.forRoot()

  ],

  bootstrap: [IonicApp],

  entryComponents: [

    // …

  ],

  providers: [

    // …

  ]

})

export class AppModule {}

 

Finally, use it inside any component or page

import { Storage } from ‘@ionic/storage’;

export class MyApp {

  constructor(private storage: Storage) { }

  …

  // set a key/value

  storage.set(‘name’, ‘Max’);

  // Or to get a key/value pair

  storage.get(‘age’).then((val) => {

    console.log(‘Your age is’, val);

  });

}

Soumik Hens Subscriber
UI Developer and Technology Enthusiast , Openweb Solutions

Tech Enthusiast and UI Developer at Openweb Solutions

Posts created 6

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top
shares