When in a native app lexical environment, the general priority is to use SQLite, because it is one of the most widely used stable document database, it also avoids some difficulties such as localstorage and INDEXEDDB, such as the system clears the data when the disk space is not enough.
If you are running on a Web or web app, you tend to use Indexeddb,websql and localstorage.
If you are using SQLite, first you have to install the Cordova-sqlite-storage plugin.
Cordova Plugin Add Cordova-sqlite-storage--save
NPM then follows the package:
NPM Install--save @ionic/storage
Then import the plugin and inject the module into the app in an array of your ngmodule imports.
Here's an example:
Import {Ionicstoragemodule} from ' @ionic/storage ';
@NgModule ({
declarations: [
//...
],
imports: [
ionicmodule.forroot (MYAPP),
Ionicstoragemodule.forroot ()//Right here
],
bootstrap: [Ionicapp],
entrycomponents: [
//...
],
providers: []
})
export class Appmodule {}
This way, if you need to use the database in that component, you can inject it through the import:
Import {Storage} from ' @ionic/storage ';
Export class MyApp {
Constructor (storage:storage) {
storage.ready (). Then (() => {
//set a key/value
Storage.set (' name ', ' Max ');
Or to get a key/value pair
storage.get (' age '). Then ((val) => {
console.log (' Your The Age is ', Val)
}} );
}
}
Configure Storage
Developers can configure the storage engine with a defined storage engine priority, or customize the configuration options to Localforage.
Note: Any custom configuration will be merged into the default configuration.
Import {Ionicstoragemodule} from ' @ionic/storage ';
@NgModule {
declarations: ...,
imports: [
ionicstoragemodule.forroot ({
name: ' __mydb ',
Driverorder: [' indexeddb ', ' sqlite ', ' Websql ']
}
],
bootstrap: ...,
entrycomponents: ...,
providers: []
})
export class Appmodule {}
The following is how the database is used: Driver gets the driver name ready () reaction store get (key) gets the corresponding data set (Key,value) sets the data remove (key) deletes the corresponding data clear () Clears all data length () Get data length keys () Fetch key ForEach (callback) Traverse database