· 6 years ago · Jun 19, 2019, 01:18 PM
1foodTblCreate = () => {
2 db.transaction(function(txn) {
3 txn.executeSql(
4 "SELECT name from sqlite_master WHERE type='table' AND name='table_food'", [],
5 function(tx, res) {
6 alert(`table_food is : ${res.rows.length}`);
7 if (res.rows.length === 0) {
8 txn.executeSql('DROP TABLE IF EXISTS table_food', []);
9 txn.executeSql(
10 "CREATE TABLE IF NOT EXISTS table_food(m_id INTEGER PRIMARY KEY AUTOINCREMENT, m_name VARCHAR(50), m_unit VARCHAR(250), cat_id VARCHAR(5), m_cal VARCHAR(6))",
11 [],
12 (tx, results) => {
13 for(var i=0; i<categoryArr.length; ++i) {
14 var cat_id = categoryArr[i].id;
15 for(var j=0;j<nutrition[cat_id].length; ++j) {
16 var m_name = nutrition[cat_id][j].name;
17 var m_unit = nutrition[cat_id][j].unit;
18 var cat_id = cat_id;
19 var m_cal = nutrition[cat_id][j].unit;
20 tx.executeSql(
21 "INSERT INTO table_food(m_name, m_unit, cat_id, m_cal) VALUES (?,?,?,?)",
22 [m_name, m_unit, cat_id, m_cal],
23 (tx, results) => {
24 alert('tableFoodData : '+JSON.stringify(results));
25 }
26 );
27 }
28 }
29 (error) => {
30 alert("Got an error in table_food"+JSON.stringify(error));
31 if (error) {
32 alert(JSON.stringify(error));
33 }
34 }
35 }
36 );
37 }
38 }
39 );
40 (error) => {
41 alert("Got an error"+JSON.stringify(error));
42 if (error) {
43 alert(JSON.stringify(error));
44 }
45
46 }
47 });
48 }