I want to develop the iPhone native web app, so I want to use the new features of HTML 5. After two days of research, I finally wrote a web SQL database operation class.
Author: lein
Emaill: lein_urg@163.com
ManyArticleIt is not written to death by passing in parameters.CodeIn this way, it is difficult to use it flexibly;
(Note: I tried ie 9 and Firefox 11. It seems that they do not support opendatabase. If you have any idea, please email me to notify me that I use safari 5.1.5 in windows, chrome 18 also passed)
In addition, I would like to remind my friends that database operations are asynchronous and you may not be able to query the data you inserted immediately. I have been doing this for a long time.
// JavaScript documentfunction _ db_query (TX) {VaR _ sqls = typeof (this. sqls) = 'undefined '? _ Db_query.sqls: This. sqls; while (_ sqls. length) {var SQL = _sqls.shift({%tx.exe cutesql (SQL [0], SQL [1], function (T, results) {try {If (results. rows. length> 0) {var DATA = []; for (VAR I = 0; I <results. rows. length; I ++) {data. push (results. rows. item (I);} This. db_cb (true, Data, 0, 0);} else if (results. rowsaffected) {try {This. db_cb (true, null, results. insertid, results. rowsaffected);} catch (e) {This. db_cb (true, null, 0, Results. rowsaffected) ;}}} catch (e) {alert (E. message) ;}}, function (T, E) {var TMP = ''; If (typeof (e) = 'object') {for (var x in E) {TMP + = x + '=' + E [x] + '\ n';} var error = TMP;} else var error = E. message; this. db_cb (false, error, 0, 0) ;}} function webdb (_ CB, _ name, _ SIZE) {This. name = _ name; this. size = _ size; this. version = arguments. length> 3? Arguments [3]: 1.0; this. DESC = arguments. length> 4? Arguments [4]: ''; this. DB = opendatabase (this. name, this. version, this. DESC, this. size * 1024); this. webdb_query = _ db_query; this. webdb_query. _ PROTO __. db_cb = _ CB; this. webdb_query. _ PROTO __. sqls = []; If (! This. DB) return false;} webdb. prototype = {db_cb: function () {}, query: function (SQL) {If (arguments. length> 1) {var Arg = arguments [1];} else var Arg = []; this. webdb_query.sqls.push ([SQL, Arg]); this. DB. transaction (this. webdb_query );}}
usage:
<! Doctype HTML>
My QQ group:
Phper & webgame & mobile development, group number: 95303036