-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
371 lines (346 loc) · 12.2 KB
/
Copy pathindex.html
File metadata and controls
371 lines (346 loc) · 12.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
<script type="text/javascript" src="js/jquery.js"></script>
<!-- <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous"/> -->
<link rel="icon" type="image/png" href="./pic/favicon.png" />
<link rel="shortcut icon" type="image/x-icon" href="./pic/favicon.ico" />
<title>Web SQL Database</title>
</head>
<body class="m30">
<!-- HTML -->
<div class="container">
<div class="flex-row-reverse">
<form>
<p>
<small>
<b>Note:</b> Please use <i>Vivaldi</i>, <i>Chrome</i>,
<i>Opera</i> or other browser that supports
<a href="https://caniuse.com/?search=web%20sql" target="_blank">
Web SQL
</a>
</small>
</p>
<fieldset>
<legend>Item name</legend>
<input type="text" name="input" id="item" class="form-control" />
<br />
<legend>Quantity</legend>
<input
type="number"
name="quantity"
id="quantity"
class="form-control"
/>
<hr />
<div class="buttons">
<button id="insert" class="btn btn-info mt10">Insert</button>
<button id="create" class="btn btn-success mt10">
Create Table
</button>
<button id="remove" class="btn btn-danger mt10">
Delete Table
</button>
<button id="list" class="btn btn-warning mt10">
Fetch Record
</button>
<!-- type="button" -->
<p>
<small>
<b>Note:</b> Table must be created first before inserting or
performing any transaction
</small>
</p>
</div>
</fieldset>
</form>
</div>
<hr />
<div class="record">Record</div>
<p>
<small>
<b>Note:</b> To change the name or quantity click to the corrsponding
value
</small>
</p>
<table class="table table-bordered table-hover" id="itemlist"></table>
</div>
<!-- JavaScript -->
<script type="text/javascript">
let db = openDatabase("itemDB", "1.0", "itemDB", 65535); // itemDB is the database name
$(function () {
loadData();
//TO CREATE TABLE - STARTS
$("#create").click(function () {
db.transaction(function (transaction) {
let item = "item";
let quantity = "#quantity";
let sql =
"CREATE TABLE items " +
"(id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT," +
"item VARCHAR(100) NOT NULL," +
"quantity INT(5) NOT NULL)";
transaction.executeSql(
sql,
undefined,
function () {
alert("Table is CREATED successfully");
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
if (
err.message ==
"could not prepare statement (1 table items already exists)"
) {
// alert("Table is already being created");
} else {
alert(err.message);
}
}
}
);
});
loadData();
});
//CREATE ENDS
//TO DELETE TABLE - STARTS
$("#remove").click(function () {
if (!confirm("Are you sure to delete this table?", "")) return;
db.transaction(function (transaction) {
let sql = "DROP TABLE items";
transaction.executeSql(
sql,
undefined,
function () {
alert("Table is DELETED successfully");
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
alert(err.message);
}
}
);
});
loadData();
});
//DELETE ENDS
//TO INSERT - STARTS
$("#insert").click(function () {
let item = $("#item").val();
let quantity = $("#quantity").val();
db.transaction(function (transaction) {
let sql = "INSERT INTO items(item, quantity) VALUES(?, ?)";
transaction.executeSql(
sql,
[item, quantity],
function () {
// alert("New item is ADDED successfully");
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
alert(err.message);
}
}
);
});
loadData();
});
//INSERT ENDS
//TO FETCH - STARTS
$("#list").click(function () {
loadData();
});
//FETCH ENDS
//TO LOAD DATA - STARTS
function loadData() {
$("#itemlist").children().remove();
$("#itemlist").append(
"<tr><td class='text-center'>" +
"<h4>ID</h4>" +
"</td><td class='text-center'>" +
"<h4>ITEMS</h4>" +
"</td><td class='text-center'>" +
"<h4>QTY</h4>" +
"</td><td class='text-center'>" +
"<h4>DEL</h4>" +
"</td></tr>"
);
db.transaction(function (transaction) {
// let sql = "SELECT * FROM items ORDER BY id DESC";
let sql = "SELECT * FROM items ORDER BY id ASC";
transaction.executeSql(
sql,
undefined,
function (transaction, result) {
if (result.rows.length) {
for (let i = 0; i < result.rows.length; i++) {
let row = result.rows.item(i);
let item = row.item;
let id = row.id;
let quantity = row.quantity;
$("#itemlist").append(
"<tr id='del" +
id +
"'><td class='text-center'>" +
id +
'</td><td id="updatename"' +
id +
'" class="updatename col-xs-8" data-id="' +
id +
'">' +
item +
'</td><td id="updateqty' +
id +
'" class="updateqty text-center" data-id="' +
id +
'">' +
quantity +
'</td><td class="text-center"><a href="#" class="btn btn-danger deleteitem" data-id="' +
id +
'">x</a>' +
// <a href="#" class="btn btn-primary updateitem" data-id="' + id +'">↑</a>
"</td>" +
"</tr>"
);
// console.log(db);
// console.log(result);
console.log("|", id, "|", item, "|", quantity, "|");
}
} else {
$("#itemlist").append(
"<tr><td colspan='4' align='center'>No item Found</td></tr>"
);
}
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
alert(err.message);
}
}
);
});
//LOAD ENDS
setTimeout(function () {
//DELETE FUNCTION STARTS
$(".deleteitem").click(function () {
let id = $(this).data("id");
db.transaction(function (transaction) {
let sql = "DELETE FROM items WHERE id=(?)";
transaction.executeSql(
sql,
[id],
function () {
// alert("DELETED successfully");
// loadData();
$("#del" + id).fadeOut("slow");
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
alert(err.message);
}
}
);
});
});
//DELETE ENDS
//UPDATE QUANTITY FUNCTION STARTS
$(".updateqty").click(function () {
let id = $(this).data("id");
let newqty = parseInt(
prompt("Kindly enter new quantity (NUMBERS ONLY)")
);
if (newqty !== null) {
db.transaction(function (transaction) {
let sql = "UPDATE items SET quantity=(?) WHERE id=(?)";
transaction.executeSql(
sql,
[newqty, id],
function () {
// alert("Updated");
$("#newqty" + id).html(newqty);
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
}
if (newqty !== Number) {
// alert("Only NUMBER aceptable");
// window.reload();
} else {
alert(err.message);
}
}
);
});
loadData();
}
});
//UPDATE ENDS
//UPDATE NAME FUNCTION STARTS
$(".updatename").click(function () {
let id = $(this).data("id");
let newitem = prompt("Kindly enter new name");
if (newitem !== null) {
db.transaction(function (transaction) {
let sql = "UPDATE items SET item=(?) WHERE id=(?)";
transaction.executeSql(
sql,
[newitem, id],
function () {
// alert("Updated");
$("#updatename" + id).html(newitem);
},
function (transaction, err) {
if (
err.message ==
"could not prepare statement (1 no such table: items)"
) {
alert("Create the table");
} else {
alert(err.message);
}
}
);
});
loadData();
}
});
//UPDATE ENDS
}, 1000);
}
});
</script>
</body>
</html>