Ionic 2 SQLite Native as a Service

Has anyone managed to create a service from SQLite-ion-native?

Thus, you can find something like addItem(param), editItem(param)which causes the appropriate utility function to handle the problem?

C Storageand SqlStorageI 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 read the docs about using SQLite in Ionic and I don’t understand how to do something according to the above, Doc: https://ionicframework.com/docs/v2/native/sqlite/

How do you do this?

+4
source share
1 answer

I don’t know what the problem is. This 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
+2
source

Source: https://habr.com/ru/post/1670342/


All Articles