/* * * Introduction to Web SQL database using *///web SQL databases entry function webdatabase () {};(function () {/** * @decription if the database Does not exist, create * @param {string} database name * @param {string} version number * @param {string} database Description * @param {number} database size *@p Aram {Function} callback function */var CurrentDb; return Webdatabase.prototype = {getcurrentdb:getcurrentdb, opendb:opendb, createtable:createtable , Insert:insert, Query:query, Update:update, Deleteitem:deleteitem, Droptable:drop Table, manualtransaction:manualtransaction}/** * @param {Object} * name {String} database name * vers Ion {string} database version number * DESC {string} database Description * Capacity {number} database capacity * Callback {Function} Create or Open database successful callback * @return {Object} created or opened database object */function Opendb (options) {var defaultoptions = {Name: ' , Version: ' 1.0 ', desc: ', capacity:1024 * 1024, Callback:function () {}}, Optionsvalue; Options = Merge (options, defaultoptions); Optionsvalue = getValues (options); CurrentDb = opendatabase.apply (null, optionsvalue); return CurrentDb; }/** * @param {string} created SQL statement * @param {Array} string data inserted into the query where the question mark is located * @param {function} succeeded callback function * @param {F Unction} failed callback function *//function ExecuteSQL (SQL, [], success, fail) {//Data table operation//} function Matchsql (SQL, TA Blename) {var reg =/\{\{(\w+) \}\}$/; sql = Sql.replace (reg, function (match, set) {return tableName; }); return SQL; }/** * @description create data table * @param {String} tableName the name of the data table to be created * @param {Object} options Description field * @param {F Unction} Success Successful callback * @param {Function} fail execution failed callback * @return {Undefined} does not return results * @example * createtable (' Student ', {ID: ' INTEGER UNIQUE PRIMARY KEY ', Name: ' TEXT '}) *-----------------------* ID | Name *-----------------------*/function CreateTable (TableName, Options, success, fail) {var sql = ' C reate table if not exists ' + TableName + ' (' + mergearrtostr (options, ') + ') ', Success = Success | | function () {alert (' success to create a table ' + tableName); }, fail = Fail | | function () {alert (' fail to create a table ' + TableName)}; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, [], success, fail); }); }/** * @param {String} tableName the name of the data table to be created * @param {Object} data inserted @param {Function} success successful Callback * @param {Function} fail execution failed callback * @return {Undefined} no return result * @example * INSERT (' student ', {id:1,name: ') Author '}) *-----------------------* id | Name * 1 | Author *-----------------------* * Function InserT (tableName, data, success, fail) {var keys = getkeys (data). Join (), values = getValues (data), Qmark = Values.map (function () {return '? '; }), sql = ' insert INTO ' + TableName + ' (' + keys + ') VALUES ("+ qmark.join () +") ", Success = suc Cess | | function () {alert (' success to insert-a item to ' + TableName)}, fail = Fail | | function () {alert (' fail to insert a item to ' + TableName)}; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, values, success, fail); }); }/** * @param {String} tableName the name of the data table to be created * @param {Array} options to query field * @param {Function} success successful Callback * @param {Function} fail execution failed callback * @return {Undefined} no return result * @example * query (' student ', ' ID ') * -----------* ID * 1 *-----------*/Function query (TableName, options, success, fail) {Array.isarray (options)? ": Options=[options]; var keys = options? Options.join (): ' * ', sql = ' SELECT ' + keys + ' from ' + tableName, Success = Success | | Function (TX, ResultSet) {alert (' success to query ' + tableName); var Resultlen = resultSet.rows.length, i = 0, result = []; for (; i < Resultlen; i++) {Result.push (ResultSet.rows.item (i)); } console.log (Result); }, fail = Fail | | function () {alert (' fail to query in ' + TableName); }; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, [], success, fail); }); }/** * @param {String} tableName the name of the data table to be created * @param {Object} options The field to update and the corresponding value * @param {Object} lookup field to update [single data] * @param {function} success successful callback * @param {function} Fail execution failed callback * @r Eturn {Undefined} no results returned * @example * update (' student ', {name: ' AUTHOR '},{name: ' id ', value:1}) *----------------- ------* ID | Name * 1 | AUTHOR *-----------------------* * Function Update (tableName, options, where, success, fail) {var up Datestr, fail, success, SQL, keys, values, Qmark; Fail = Fail | | function () {alert (' fail to update ' + tableName); }, Success = Success | | function () {alert (' Success to update ' + tableName); Console.log (arguments); The keys = Getkeys (options); Values = getValues (options); Qmark = Keys.map (function () {return '? '; }); options = {}; Keys.foreach (function (key, index) {Options[key] = Qmark[index]; }); Values.push (WHEre.value); Updatestr = mergearrtostr (options, ' = '), sql = ' Update ' + tableName + ' Set ' + Updatestr + ' where ' + where. name + ' =? ‘; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, values, success, fail); }); }/** * @param {String} tableName the name of the data table to be created * @param {Object} The lookup field to delete [single data] * @param {Function} success Function Callback * @param {Function} fail execution failed callback * @return {Undefined} no return result * @example * deleteItem (' student ', {name: ' ID ', value:1}) *-----------------------* id | Name *-----------------------*/function DeleteItem (TableName, where, success, fail) {var sql = ' De Lete from ' + TableName + ' where ' + where.name + ' =? ', Success = Success | | Function (TX, ResultSet) {alert (' success to delete a "item in ' + TableName); var len = resultSet.rows.length, i = 0, result = []; for (; i < Len; i++) {Result.push (ResultSet.rows.item (i)); } console.log (resultset.rowsaffected); }, fail = Fail | | function () {alert (' fail to delete a item in ' + TableName)}; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, [Where.value], success, fail); }); }/** * @param {String} tableName the name of the data table to be deleted * @param {function} Delete successful callback * @param {function} Delete failed callback * @return {Undefined} no results returned * @example * droptable (' student ') *-------------------*------------------- */function Droptable (TableName, success, fail) {var sql = ' drop table ' + tableName, success = Success | | function () {Console.log (' droptable: ', arguments); }, fail = Fail | | function () {}; sql = matchsql (sql, tableName); Console.log (SQL); Currentdb.transaction (Function (TX) {tx.executesql (SQL, [], success, fail); }); }/** * @description manually executed SQL * @param {String} SQL statement * @param {array| | String} Required Data * @param {function} callback after successful execution * @param {function} execution failed callback * @return {Undefined} no results returned * @ex ample * var sql = ' CREATE table if not EXISTS student (ID INTEGER not NULL PRIMARY key,name TEXT) '; * manualtransaction (SQL); *-----------------------* id | Name *-----------------------*/function manualtransaction (SQL, values, success, fail) {values = Array.is Array (values)? Values: [Values]; Currentdb.transaction (Function (TX) {tx.executesql (SQL, values, success, fail); }); }/** * @return {Object} current active database */function Getcurrentdb () {return currentdb; } function merge (target, source) {var rEsult = {}; for (var i in source) {if (Source.hasownproperty (i)) {result[i] = Target[i] | | source[i]; }} return result; } function GetValues (obj) {var keys = Getkeys (obj), length = keys.length, values = []; for (var i = 0; i < length; i++) {values[i] = obj[keys[i]]; } return values; } function Getkeys (obj) {if (typeof obj!== ' object ') return []; return Object.keys (obj); } function Mergearrtostr (options, connector) {var keys = Getkeys (options), values = getValues (option s), Updatestr; UPDATESTR = Keys.map (function (value, index) {return value + connector + Values[index]; }); return Updatestr.join (); }})();
Implement data management using a local database management system:
How to use:
var localDb = new Webdatabase (); Localdb.opendb ();
Web SQL Database Local databases