HTML5 web SQL dynamically creates database tables, html5sql
SmcsServices. service ('visitinstitutioncacheservice ', [' $ rootScope ',' $ http', 'baseurl', 'cesceservice ', function ($ rootScope, $ http, BaseURL, DataSourceService) {var service = {synVisit: function (synvtdatetime) {$ http ({method: 'post', cache: false, url: BaseURL + 'syscommoncodetransform/findVisitInstitution. do ', params: {datetime: synvtdatetime }}). success (function (response, status, headers, config) {Try {var obj = response. visitInstitution; var synobj = response. synvisitlist; // optimized for dynamic table var objstr = JSON after test and development. stringify (obj [0]); console. log ("obj [0] ====" + objstr); var objjson = JSON. parse (objstr); for (var key in objjson) {console. log ("objjson key =" + key);} // after testing and development, optimize it to dynamically create a table var db = DataSourceService. getConnection (); // console. log ("synvisitlist synobj [0]. syndatetime = "+ synobj [0]. syndatetime); // var db = op EnDatabase ('mydb14', '1. 0', 'test db', 6*1024*1024); DB. transaction (function (tx) using tx.exe cuteSql ('create Table If Not Exists VisitInstitution (id, AKB020, AKB021, AKA020, AKA101 )'); tx.exe cuteSql ('delete FROM syncachecode WHERE syntablename = "VisitInstitution" INTO cuteSql ('insert INTO syncachecode (partition, syndatetime) VALUES ("VisitInstitution", "'+ synobj [0]. syndatetime + '"); for (var I = 0; I <obj. length; I ++) {var objAKB020 = obj [I]. AKB020 + ""; var objAKB021 = obj [I]. AKB021 + ""; var objAKA020 = obj [I]. AKA020 + ""; var objAKA101 = obj [I]. AKA101 + ""; if (synvtdatetime! = "00:00:00") {// The deletion statement is not executed during initialization. // Delete the data before inserting it during data synchronization. Tx.exe cuteSql ('delete FROM VisitInstitution where id =? ', Too objpolici=.id=00000000tx.exe cuteSql ('insert INTO VisitInstitution (id, AKB020, AKB021, AKA020, AKA101) VALUES ("' + obj [I]. ID + '","' + objAKB020 + '","' + objAKB021 + '","' + objAKA020 + '","' + objAKA101 + '") ') ;}}) ;}catch (e) {console. log (e. name + ":" + e. message) ;}}) ;}, initSysCode: function () {var syndatetime = ""; // var db = openDatabase ('mydb14', '1. 0 ', 'test db', 6*1024*1024); var DB = DataSourceService. getConnection (); db. transaction (function (tx) using tx.exe cuteSql ('select * from syncachecode where syntablename = "VisitInstitution" ', [], function (tx, results) {var len = results. rows. length; syndatetime = results. rows. item (0) ['syndatetime']; console. log ("synchronization time of the front-end database CenterReimburs is" + results. rows. item (0) ['syndatetime']); service. synVisit (syndatetime) ;}, null) ;}} return service ;}]);
Console:
Obj [0] ==={ "AKA101": "11", "AKB020": "02162001", "AKB021": "lingjing community health service station, West chang'an Street, Xicheng district ", "ID": "11010602162001", "AKA020": "xcqxcajdljsqwsfwz "}
The key of objjson is === AKA101
The key of objjson is === AKB020
The key of objjson is === AKB021
The key of objjson is ===id
The key of objjson is === AKA020
Dynamic table creation in web db
Web SQL dynamically creates database tables and synchronizes them with backend databases.
When a table is created for the first time, mydatetime = "1970", and the date and time returned by the background in the future are used as the synchronization time.
Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.