Reputation: 9517
Anyone had success creating a service out of the SQLite ionic-native?
So one could end up with something like addItem(param)
, editItem(param)
, which calls the respective service function to handle the task?
With Storage
and SqlStorage
, I could do something like this:
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]);
}
}
I've been reading the docs about using the SQLite in Ionic, and I'm not understanding how to do something along the lines of the above, Doc: https://ionicframework.com/docs/v2/native/sqlite/
How do you do it?
Upvotes: 4
Views: 832
Reputation: 2993
Not sure what the problem is.. here is how i use it
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
Upvotes: 2