Ionic2 calls the local SQlite instance and ionic2sqlite instance

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.