Ionic3学习笔记(六)存储之使用 SQLite

https://blog.csdn.net/metaphorxi/article/details/78232698

本文为原创文章,转载请标明出处

目录

  1. 安装
  2. CURD操作

1. 安装

命令行输入

ionic cordova plugin add cordova-sqlite-storage
npm install --save @ionic-native/sqlite
  • 1
  • 2

在 ./src/app/app.module.ts 中添加

import {SQLite} from "@ionic-native/sqlite";
  • 1

在 provides 中添加

SQLite,
  • 1

2. CURD操作

user.ts

export class User {

  username: string;
  password: string;
  gender: boolean;
  age: number;
  intro: string;
  email: string;
  phone: string;
  location: string;

  constructor() {

  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16

data.ts

import {SQLite, SQLiteObject} from "@ionic-native/sqlite";

export class DataProvider {

  myAppDatabase: SQLiteObject;

  constructor(private sqlite: SQLite) {

  }

  initDatabase() {
    this.sqlite.create({
      name: 'myApp.db',
      location: 'default'
    }).then((database: SQLiteObject) => {
      database.executeSql('CREATE TABLE IF NOT EXISTS users(email VARCHAR(320) PRIMARY KEY, username VARCHAR(20) NOT NULL, password VARCHAR(30) NOT NULL, gender BOOLEAN, age TINYINT, intro VARCHAR(300), phone CHAR(11), location VARCHAR(100));', {}).then(() => console.log('init database successfully')).catch(e => console.log(e));
      this.myAppDatabase = database;
    })
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22

user-data.ts

import {Injectable} from '@angular/core';

import {SQLiteObject} from "@ionic-native/sqlite";

import {DataProvider} from "../data/data";

import {User} from "./user";

@Injectable()
export class UserDataProvider {

  database: SQLiteObject;

  constructor(private dataProvider: DataProvider) {
    this.database = this.dataProvider.myAppDatabase;
  }

  insertIntoUserTable(user: User) {
    this.database.executeSql('INSERT INTO users VALUES (?, ?, ?, NULL, NULL, NULL, NULL, NULL);', [user.email, user.username, user.password]).then(() => console.log('insert into users table successfully')).catch(e => console.log(e));
  }

  queryUserTable() {
    this.database.executeSql('SELECT * FROM users;', {}).then(() => console.log('query users table successfully')).catch(e => console.log(e));
  }

  updateUserTable(user: User) {
    this.database.executeSql('UPDATE users SET username=?, password=?, gender=?, age=?, intro=?, phone=?, location=? WHERE email=?;', [user.username, user.password, user.gender, user.age, user.intro, user.phone, user.location, user.email]).then(() => console.log('update users table successfully')).catch(e => console.log(e));
  }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31

更多可详见 
1. Ionic Native - SQLite 
2. GitHub - Cordova-sqlite-storage

如有不当之处,请予指正,谢谢~

扫描二维码关注公众号,回复: 1594884 查看本文章

猜你喜欢

转载自blog.csdn.net/fangquan1980/article/details/80681267