Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import { Injectable } from 'angular2/core';
- import { Storage, SqlStorage, Platform } from 'ionic-angular';
- @Injectable()
- export class Stanservice {
- static get parameters() {
- return [
- [Platform]
- ]
- }
- constructor(platform) {
- this.data = null;
- this.storage = new Storage(SqlStorage);
- this.initDB();
- // this.addDummy();
- }
- initDB() {
- this.storage.query('CREATE TABLE IF NOT EXISTS category (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, note TEXT)')
- .then((data) => {
- console.log("Table create -> " + JSON.stringify(data.res));
- }, (error) => {
- console.log("Error -> " + JSON.stringify(error.err));
- });
- }
- deleteTable() {
- this.storage.query("DELETE FROM category")
- .then((data) => {
- console.log('Deleted db Successfully');
- }, (error) => {
- console.log('Error -> ', JSON.stringify(error.res));
- })
- }
- addDummy() {
- // add not more than 5 categories
- // should be removed eventually
- this.storage.query("SELECT * FROM category")
- .then((data) => {
- this.storage.query("INSERT INTO category (name, note, type) VALUES ('Housing', 'Greatness', 'income')").then((data) => {
- console.log(JSON.stringify(data.res));
- }, (error) => {
- console.log("ERROR -> ", JSON.stringify(error.err));
- })
- })
- };
- // this works
- saveCat(data) {
- var myarray = [];
- for (var elem in data) {
- myarray.push(data[elem])
- }
- console.log(myarray);
- this.storage.query("INSERT INTO category (name, note, type) VALUES(?,?,?)", myarray).then((data) => {
- console.log("Save Cat was fired");
- console.log("Success", JSON.stringify(data.res));
- }, (error) => {
- console.log("Error -->", error.err);
- })
- };
- getCat() {
- this.storage.query('SELECT * FROM category')
- .then((data) => {
- var category = [];
- if (data.res.rows.length > 0) {
- for (var i = 0; i < data.res.rows.length; i++) {
- category.push({
- name: data.res.rows.item(i).name,
- type: data.res.rows.item(i).type,
- note: data.res.rows.item(i).note
- });
- }
- }
- // console.log(JSON.stringify(category));
- return category; // is this correct?
- }, (error) => {
- console.log('Error -> ' + JSON.stringify(error.err));
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement