-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase_functions.js
60 lines (55 loc) · 1.5 KB
/
database_functions.js
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
import SQLite from 'react-native-sqlite-storage';
const db = SQLite.openDatabase(
{
name: 'TestDB',
location: 'default'
},
() => {},
error => {console.log(error)}
);
const setData = (date, label, image) => {
const promise = new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'INSERT INTO Logs2 (Date, Label, Image) VALUES (?, ?, ?);',
[date, label, image],
(result) => {
console.log("INSERTED: ", result);
resolve(result);
},
(err) => {
reject(err);
}
);
});
});
return promise;
}
const getData = (resolveFunc) => { // retrieves data from table
let dates = [];
let labels = [];
let images = [];
// create promise that selects all data from table
const promise = new Promise((resolve, reject) => {
db.transaction(tx => {
tx.executeSql(
'SELECT * FROM Logs2;',
[],
(tx, results) => {
console.log("SELECTED: ", results);
for (let i = 0; i < results.rows.length; i++) {
dates.push(results.rows.item(i).Date);
labels.push(results.rows.item(i).Label);
images.push(results.rows.item(i).Image);
}
resolve({dates, labels, images});
resolveFunc([dates, labels, images]);
},
(err) => {
reject(err);
}
);
});
});
}
export {db, setData, getData};