2017-02-20 29 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