-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase-extensions.js
More file actions
79 lines (65 loc) · 1.92 KB
/
database-extensions.js
File metadata and controls
79 lines (65 loc) · 1.92 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
// DOPE
// Database Object Promise Extension
// end DOPE
function reportError(msg) {
app.Alert("Error: " + msg);
console.log("Error: " + msg);
}
function createGui() {
const Wb = 0.6; // button width
const Hb = 0.1; // button height
const layout = app.CreateLayout("Linear", "VCenter,FillXY");
const add = app.CreateButton("Add to Database", Wb, Hb);
const remove = app.CreateButton("Remove from Database", Wb, Hb);
const deleteDb = app.CreateButton("Delete Database", Wb, Hb);
results = app.CreateText("", 0.9, 0.4, "Multiline");
add.SetOnTouch(function () {
mydb.ExecuteSql("INSERT INTO test_table (data, data_num) VALUES (?,?)", ["test", 100])
.catch(function (msg) {
reportError(msg)
})
updateResults();
});
remove.SetOnTouch(function () {
mydb.ExecuteSql("DELETE FROM test_table WHERE id > 3")
updateResults();
});
deleteDb.SetOnTouch(function () {
mydb.Delete();
updateResults();
});
results.SetMargins(0, 0.1, 0, 0);
results.SetBackColor("#ff222222");
results.SetTextSize(18);
layout.AddChild(add);
layout.AddChild(remove);
layout.AddChild(deleteDb);
layout.AddChild(results);
app.AddLayout(layout);
}
//Called when application is started.
function OnStart() {
createGui();
//Create or open a database called "MyData".
mydb = app.OpenDatabase("MyData");
// db2 = new DB(db);
//Create a table (if it does not exist already).
mydb.ExecuteSql("CREATE TABLE IF NOT EXISTS test_table " +
"(id integer primary key, data text, data_num integer)");
//Get all the table rows.
updateResults();
}
function updateResults() {
results.SetText("");
mydb.ExecuteSql('select * from test_table;')
.then(function (res) {
var s = "";
for (var item of res) {
s += item.id + ", " + item.data + ", " + item.data_num + "\n";
}
results.SetText(s);
})
.catch(function (msg) {
reportError(msg)
})
}