2017-02-20 58 views
4

任何人都能成功创建SQLite离子本地服务?Ionic 2 SQLite本地化即服务

所以人们可能会得到类似addItem(param),editItem(param),它调用各自的服务功能来处理任务?

随着StorageSqlStorage,我可以做这样的事情:

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]); 
    } 
} 

我一直在阅读的文档关于离子使用SQLite的,而且我不理解怎么办沿线的东西,Doc:https://ionicframework.com/docs/v2/native/sqlite/

你是如何做到的?

+1

@suraj你一定要明白这个问题是关于使用SQLite的原住民,没有别的了吧? – Rexford

+0

您使用的示例实际上是服务CategoryService,您的问题是什么?你需要解释它是如何工作的?不要像Json – LeRoy

+0

一样对待SQLite @LeRoy不,它不是如何工作的。我在问我如何使用SQLite Native Plugin来做类似的事情,创建一个服务。 – Rexford

回答

2

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

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