Advertisement
seanmavley

Untitled

May 13th, 2016
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.46 KB | None | 0 0
  1. import { Injectable } from 'angular2/core';
  2. import { Storage, SqlStorage, Platform } from 'ionic-angular';
  3.  
  4. @Injectable()
  5. export class Stanservice {
  6. static get parameters() {
  7. return [
  8. [Platform]
  9. ]
  10. }
  11.  
  12. constructor(platform) {
  13. this.data = null;
  14. this.storage = new Storage(SqlStorage);
  15. this.initDB();
  16. // this.addDummy();
  17. }
  18.  
  19. initDB() {
  20. this.storage.query('CREATE TABLE IF NOT EXISTS category (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, type TEXT, note TEXT)')
  21. .then((data) => {
  22. console.log("Table create -> " + JSON.stringify(data.res));
  23. }, (error) => {
  24. console.log("Error -> " + JSON.stringify(error.err));
  25. });
  26. }
  27.  
  28. deleteTable() {
  29. this.storage.query("DELETE FROM category")
  30. .then((data) => {
  31. console.log('Deleted db Successfully');
  32. }, (error) => {
  33. console.log('Error -> ', JSON.stringify(error.res));
  34. })
  35. }
  36.  
  37. addDummy() {
  38. // add not more than 5 categories
  39. // should be removed eventually
  40. this.storage.query("SELECT * FROM category")
  41. .then((data) => {
  42. this.storage.query("INSERT INTO category (name, note, type) VALUES ('Housing', 'Greatness', 'income')").then((data) => {
  43. console.log(JSON.stringify(data.res));
  44. }, (error) => {
  45. console.log("ERROR -> ", JSON.stringify(error.err));
  46. })
  47.  
  48. })
  49. };
  50.  
  51. // this works
  52. saveCat(data) {
  53. var myarray = [];
  54.  
  55. for (var elem in data) {
  56. myarray.push(data[elem])
  57. }
  58. console.log(myarray);
  59. this.storage.query("INSERT INTO category (name, note, type) VALUES(?,?,?)", myarray).then((data) => {
  60. console.log("Save Cat was fired");
  61. console.log("Success", JSON.stringify(data.res));
  62. }, (error) => {
  63. console.log("Error -->", error.err);
  64. })
  65. };
  66.  
  67. getCat() {
  68. this.storage.query('SELECT * FROM category')
  69. .then((data) => {
  70. var category = [];
  71. if (data.res.rows.length > 0) {
  72. for (var i = 0; i < data.res.rows.length; i++) {
  73. category.push({
  74. name: data.res.rows.item(i).name,
  75. type: data.res.rows.item(i).type,
  76. note: data.res.rows.item(i).note
  77. });
  78. }
  79. }
  80. // console.log(JSON.stringify(category));
  81. return category; // is this correct?
  82. }, (error) => {
  83. console.log('Error -> ' + JSON.stringify(error.err));
  84. });
  85. }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement