selftest_db.js 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. var st_db;
  2. function getDatabase(){
  3. if(!st_db)
  4. st_db = LocalStorage.openDatabaseSync("selftest_db", "1.0", "selftest history records", 2048*1000);
  5. return st_db
  6. }
  7. function initDatabase() {
  8. st_db = LocalStorage.openDatabaseSync("selftest_db", "1.0", "selftest history records", 2048*1000);
  9. try {
  10. st_db.transaction( function(tx) {
  11. tx.executeSql('CREATE TABLE IF NOT EXISTS test_table(\
  12. dt TEXT primary key, \
  13. time TEXT, \
  14. target REAL, \
  15. interval INTEGER, \
  16. threshold REAL, \
  17. begin REAL, \
  18. end REAL, \
  19. delta REAL, \
  20. passed INTEGER )');
  21. })
  22. } catch (err) {
  23. console.log("Error creating table in database: " + err)
  24. };
  25. }
  26. function readData(tablename_str) {
  27. var res;
  28. //let sql_str = 'select * from ${tablename_str}'
  29. if(!st_db) { return; }
  30. try{
  31. st_db.transaction( function(tx) {
  32. res = tx.executeSql('select * from '+ tablename_str);
  33. })
  34. }catch(err){
  35. console.log("Error table:"+tablename_str+" not exist in database: " + err)
  36. return;
  37. }
  38. return res;
  39. }
  40. function readRecord(date_str) {
  41. var res;
  42. //let sql_str = 'select * from ${tablename_str}'
  43. if(!st_db) { return; }
  44. try{
  45. st_db.transaction( function(tx) {
  46. res = tx.executeSql('SELECT * FROM test_table WHERE dt = '+ date_str);
  47. })
  48. }catch(err){
  49. console.log("Error Record:"+date_str+" not exist in test_table: " + err)
  50. return;
  51. }
  52. return res;
  53. }
  54. function insertRecord(record) {
  55. var res = "Error";
  56. if(!st_db) { return res }
  57. var today = new Date();
  58. //日期
  59. var DD = String(today.getDate()).padStart(2, '0'); // 获取日
  60. var MM = String(today.getMonth() + 1).padStart(2, '0'); //获取月份,1 月为 0
  61. var yyyy = today.getFullYear(); // 获取年
  62. // 时间
  63. var hh = String(today.getHours()).padStart(2, '0'); //获取当前小时数(0-23)
  64. var mm = String(today.getMinutes()).padStart(2, '0'); //获取当前分钟数(0-59)
  65. var ss = String(today.getSeconds()).padStart(2, '0'); //获取当前秒数(0-59)
  66. let date = yyyy + '-' + MM + '-' + DD;
  67. let time = hh + ':' + mm + ':' + ss;
  68. try{
  69. st_db.transaction( function(tx) {
  70. var result = tx.executeSql('INSERT OR REPLACE INTO test_table VALUES (?,?,?,?,?,?,?,?,?)',
  71. [date, time, record.target, record.interval, record.threshold, record.begin, record.end, record.delta, record.passed]);
  72. if (result.rowsAffected > 0) {
  73. res = "OK";
  74. } else {
  75. res = "Error";
  76. }
  77. })
  78. }catch(err){
  79. console.log("Error insert record in test_table: " + err)
  80. res = "Error";
  81. }
  82. return res
  83. }
  84. function deleteRecord(date_str) {
  85. var res = "Error";
  86. var today = new Date(date_str);
  87. var DD = String(today.getDate()).padStart(2, '0'); // 获取日
  88. var MM = String(today.getMonth()+1).padStart(2, '0'); //获取月份,1 月为 0
  89. var yyyy = today.getFullYear(); // 获取年
  90. let date = yyyy + MM + DD;
  91. let table_name = 'data_'+date
  92. console.log("DeleteRecord in date_str: " + date_str)
  93. console.log("DeleteRecord in table_name: " + table_name)
  94. if(!st_db) { return res }
  95. try{
  96. st_db.transaction( function(tx) {
  97. tx.executeSql('DELETE FROM test_table WHERE dt =\"'+date_str+'\"');
  98. var result = tx.executeSql('SELECT * FROM sqlite_master WHERE type="table" AND name=\"'+table_name+'\"')
  99. if(result.rows.length > 0){
  100. tx.executeSql('DROP TABLE '+table_name);
  101. }
  102. })
  103. }catch(err){
  104. console.log("Error insert record in test_table: " + err)
  105. return res
  106. }
  107. res = "OK";
  108. return res
  109. }
  110. function insertData(data_arry) {
  111. var res = "Error";
  112. if(!st_db) { return res }
  113. var today = new Date();
  114. var DD = String(today.getDate()).padStart(2, '0'); // 获取日
  115. var MM = String(today.getMonth() + 1).padStart(2, '0'); //获取月份,1 月为 0
  116. var yyyy = today.getFullYear(); // 获取年
  117. let date = yyyy + MM + DD;
  118. let table_name = 'data_'+date
  119. console.log("insertData tablename: " + table_name)
  120. try {
  121. st_db.transaction( function(tx) {
  122. var result = tx.executeSql('SELECT * FROM sqlite_master WHERE type="table" AND name=\"'+table_name+'\"')
  123. console.log("insertData 1111 result.rows.length: " + result.rows.length)
  124. var table_exist = false
  125. if(result.rows.length > 0){
  126. table_exist=true
  127. }
  128. if(true == table_exist){
  129. console.log("insertData 1111 true table_exist: " + table_exist)
  130. st_db.transaction( function(tx) {
  131. tx.executeSql('DELETE FROM '+ table_name);
  132. tx.executeSql('UPDATE sqlite_sequence SET seq = 0 WHERE name =\"'+table_name+'\"');
  133. })
  134. }else{
  135. console.log("insertData 1111 false table_exist: " + table_exist)
  136. st_db.transaction( function(tx) {
  137. tx.executeSql('CREATE TABLE IF NOT EXISTS '+ table_name +'(\
  138. ID INTEGER primary key AUTOINCREMENT, \
  139. step INTEGER, \
  140. pressure REAL)');
  141. })
  142. }
  143. console.log("insertData 2222 tablename: " + table_name)
  144. st_db.transaction( function(tx) {
  145. //var i;
  146. for(var i=0; i< data_arry.length; i++){
  147. tx.executeSql('INSERT INTO '+ table_name +' (step,pressure) VALUES ('+data_arry[i].step +','+ data_arry[i].pressure+')');
  148. }
  149. })
  150. })
  151. } catch (error) {
  152. console.log("Error SELECT table in sqlite_master: " + error)
  153. return res;
  154. };
  155. /*
  156. if(table_exist){
  157. try {
  158. st_db.transaction( function(tx) {
  159. tx.executeSql('DELETE * FROM '+ table_name);
  160. })
  161. } catch (error1) {
  162. console.log("Error1 DELETE * FROM table insertData : " + error1)
  163. return res;
  164. };
  165. }else{
  166. try {
  167. st_db.transaction( function(tx) {
  168. tx.executeSql('CREATE TABLE IF NOT EXISTS '+ table_name +'(\
  169. id INTEGER primary key AUTOINCREMENT, \
  170. step INTEGER, \
  171. pressure REAL)');
  172. })
  173. } catch (error2) {
  174. console.log("Error creating table insertData in database: " + error2)
  175. return res;
  176. };
  177. }
  178. try {
  179. st_db.transaction( function(tx) {
  180. for(i=0; i< data_arry.length; i++){
  181. tx.executeSql('INSERT INTO '+ table_name +'(?,?)', [NULL, data_arry[i].step, data_arry[i].pressure]);
  182. }
  183. })
  184. } catch (error3) {
  185. console.log("Error INSERT INTO "+ table_name +"(?,?):" + error3)
  186. return res
  187. };
  188. */
  189. res = "OK";
  190. return res
  191. }
  192. /*
  193. function deleteData(date_str){
  194. let table_name = date_str+'_data'
  195. }
  196. */