· 5 years ago · Aug 19, 2020, 02:14 PM
1//note, below is a code example for cordova. This does not entail the entire scope of the script or all of cordova implementations.
2// install : cordova plugin add https://github.com/litehelpers/Cordova-sqlite-storage.git
3// link : https://github.com/litehelpers/Cordova-sqlite-storage
4
5angular.module('ngCordova.plugins.sqlite', [])
6
7 .factory('$cordovaSQLite', ['$q', '$window', function ($q, $window) {
8
9 return {
10 openDB: function (options, background) {
11
12 if (angular.isObject(options) && !angular.isString(options)) {
13 if (typeof background !== 'undefined') {
14 options.bgType = background;
15 }
16 return $window.sqlitePlugin.openDatabase(options);
17 }
18
19 return $window.sqlitePlugin.openDatabase({
20 name: options,
21 bgType: background
22 });
23 },
24
25 execute: function (db, query, binding) {
26 var q = $q.defer();
27 db.transaction(function (tx) {
28 tx.executeSql(query, binding, function (tx, result) {
29 q.resolve(result);
30 },
31 function (transaction, error) {
32 q.reject(error);
33 });
34 });
35 return q.promise;
36 },
37
38 insertCollection: function (db, query, bindings) {
39 var q = $q.defer();
40 var coll = bindings.slice(0); // clone collection
41
42 db.transaction(function (tx) {
43 (function insertOne() {
44 var record = coll.splice(0, 1)[0]; // get the first record of coll and reduce coll by one
45 try {
46 tx.executeSql(query, record, function (tx, result) {
47 if (coll.length === 0) {
48 q.resolve(result);
49 } else {
50 insertOne();
51 }
52 }, function (transaction, error) {
53 q.reject(error);
54 return;
55 });
56 } catch (exception) {
57 q.reject(exception);
58 }
59 })();
60 });
61 return q.promise;
62 },
63
64 nestedExecute: function (db, query1, query2, binding1, binding2) {
65 var q = $q.defer();
66
67 db.transaction(function (tx) {
68 tx.executeSql(query1, binding1, function (tx, result) {
69 q.resolve(result);
70 tx.executeSql(query2, binding2, function (tx, res) {
71 q.resolve(res);
72 });
73 });
74 },
75 function (transaction, error) {
76 q.reject(error);
77 });
78
79 return q.promise;
80 },
81
82 deleteDB: function (dbName) {
83 var q = $q.defer();
84
85 $window.sqlitePlugin.deleteDatabase(dbName, function (success) {
86 q.resolve(success);
87 }, function (error) {
88 q.reject(error);
89 });
90
91 return q.promise;
92 }
93 };
94 }]);
95
96
97
98
99
100// when typing in the first name box to make a test database call.
101function skycapTest2() {
102 console.log("skycaptestFunction");
103var db = null;
104var example = angular.module('starter', ['ionic', 'ngCordova'])
105 .run(function($ionicPlatform, $cordovaSQLite) {
106 $ionicPlatform.ready(function() {
107 if(window.cordova && window.cordova.plugins.Keyboard) {
108 cordova.plugins.Keyboard.hideKeyboardAccessoryBar(true);
109 }
110 if(window.StatusBar) {
111 StatusBar.styleDefault();
112 }
113 db = $cordovaSQLite.openDB("RTest.db");
114 $cordovaSQLite.execute(db, "CREATE TABLE IF NOT EXISTS people (id integer primary key, firstname text, lastname text)");
115 });
116 });
117
118//my script to do a test call
119
120 // where i think im getting the error
121 example.controller("ExampleController", ($scope, $cordovaSQLite) => {
122 $scope.insert = function(firstname, lastname) {
123 const query = "INSERT INTO people (firstname, lastname) VALUES (?,?)";
124 $cordovaSQLite.execute(db, query, [firstname, lastname])
125 .then((res) => console.log("INSERT ID -> " + res.insertId))
126 .catch(err => console.error(err));
127 }
128 $scope.insert('Brandon','R')
129 })
130
131
132 $scope.select = function(lastname) {
133 var query = "SELECT firstname, lastname FROM people WHERE lastname = ?";
134 $cordovaSQLite.execute(db, query, [lastname]).then(function(res) {
135 if(res.rows.length > 0) {
136 console.log("SELECTED -> " + res.rows.item(0).firstname + " " + res.rows.item(0).lastname);
137 } else {
138 console.log("No results found");
139 }
140 }, function (err) {
141 console.error(err);
142 });
143 }
144 alert('we made it?')
145
146
147}
148
149
150
151
152/*
153 function skycaptext2() {
154 console.log("skycaptext2");
155 var db = null;
156 document.addEventListener('deviceready', function() {
157 db = window.sqlitePlugin.openDatabase({
158 name: 'RealeTest.db',
159 location: 'default',
160 });
161 });
162 //reale test for skycaps
163 db.transaction(function(tx) {
164 tx.executeSql('CREATE TABLE IF NOT EXISTS DemoTable (name, score)');
165 tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Alice', 101]);
166 tx.executeSql('INSERT INTO DemoTable VALUES (?,?)', ['Betty', 202]);
167 }, function(error) {
168 alert("no go bro");
169 console.log('demo Transaction ERROR: ' + error.message);
170 }, function() {
171 console.log('demo Populated database OK');
172 Alert("you got boost power");
173 });
174}
175*/