Ionic 2 SQLite Native as a Service

任何人都成功地从SQLite离子本机创建服务?

那么最终可能会出现像addItem(param),editItem(param)之类的东西,它会调用相应的服务函数来处理任务?

使用Storage和SqlStorage,我可以这样做:

import {Injectable} from '@angular/core';
import { Storage, SqlStorage } from 'ionic-angular';

@Injectable()
export class CategoryService {     
  constructor() {

    this.storage = new Storage(SqlStorage);

    this.storage.query('CREATE TABLE IF NOT EXISTS category (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT)');
  }

  saveCategory(data) {
    let sql = 'INSERT INTO category (name, type) VALUES (?, ?)';
    return this.storage.query(sql, [data.name, data.type]);
  }
}

我一直在阅读有关在Ionic中使用SQLite的文档,而我不了解如何按照上述内容进行操作,Doc:https://ionicframework.com/docs/v2/native/sqlite/

你怎么做呢?

最佳答案 不知道问题是什么..这是我如何使用它

import { Injectable } from '@angular/core';
import { SQLite } from 'ionic-native';

@Injectable()
export class DBService {

    private db: SQLite;

    constructor() {
        this.db = null;
    };

    public open() {
        if (window.sqlitePlugin) {
            this.db = new SQLite();
        } else { //handle in desktop if needed }
    };
 }

// other methods
点赞