For the Web SQL database operation class, the query statement is passed as a parameter.

Source: Internet
Author: User

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

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.