Ionic2 calls the local SQlite instance and ionic2sqlite instance
Ordinary apps can use ionic built-in Storage to store key-value pairs, but sometimes encounter some strange requirements. For example, a netizen commented on how to call local Sqlite to execute SQL statements for an offline App. The Problem description is clear, and the code is displayed directly.
It should be noted that SQLite is the built-in database storage method of mobile phones. In Ionic2, you need to install the corresponding plug-ins and installation packages. The process is simple.
Step 1
Install the plug-in and add it to the Project
$ ionic plugin add cordova-sqlite-storage$ npm install --save @ionic-native/sqlite
Step 2
Add the service to src/app. moudle. ts
...import { SQLite } from '@ionic-native/sqlite';...providers: [ ... SQLite]...
Step 3
Generally, this step should be encapsulated into a public service or tool class. Class is used to create a database, call a database, CRUD, and other methods. Here is just a description of the principle, direct call
Import {Component} from '@ angular/core'; import {SQLite, SQLiteObject} from' @ ionic-native/sqlite '; @ Component ({selector: 'page-hello-ionic ', templateUrl: 'hello-ionic.html'}) export class HelloIonicPage {constructor (private sqlite: SQLite) {} database: SQLiteObject; ngOnInit () {this. initDB ();} initDB () {this. sqlite. create ({name: 'Data. db', location: 'default '}). then (db: SQLiteObject) =>{ db.exe cuteSql ('create table t_log (name VARCHAR (32) ', {}) // create a table. then () => console. log ('executed SQL ')). catch (e => console. log (e); this. database = db; db.exe cuteSql ("insert into t_log values ('000000')", {}); // insert data }). catch (e => console. log (e);} // query () {let results = this.database.exe cuteSql ("select * from t_log" ,{}); alert (data. rows. length); alert (data. rows. item (0 ). name );})}}
Last step
In this step, the app must be generated and installed on the mobile phone to obtain the result. After all, it is called the built-in SQLite of the mobile phone.
ionic build android
Use the following command to build an APP and install it on your mobile phone.
The above is all the content of this article. I hope it will be helpful for your learning and support for helping customers.