webSQL 经常使用的几个必要函数
仅供参考:
var html5rocks = {};html5rocks.webdb = {};html5rocks.webdb.db = null; //将sqldb存到一个全局变量中openDB();//创建databasefunction openDB(){ var dbSize = 5 * 1024 * 1024; // 5MB html5rocks.webdb.db = openDatabase("JoyOfficialApp", "1.0", "JoyOfficialApp DB", dbSize); createTable();}//创建表function createTable(){ var db = html5rocks.webdb.db; db.transaction(function(tx) { tx.executeSql("CREATE TABLE IF NOT EXISTS MYFOLLOWINFO(ID INTEGER PRIMARY KEY ASC,INFOID TEXT,TYPETAG TEXT)", []); tx.executeSql("CREATE TABLE IF NOT EXISTS UPLOADFOLLOW(ID INTEGER PRIMARY KEY ASC,INFOID TEXT,TYPETAG TEXT)", []); },dbOnError,dbOnSuccess);}//执行sql发生异常时function dbOnError(tx, e){ console.log("There has been an error: " + e.message);}//sql调用成功后的函数function dbOnSuccess(tx, r) { //alert("create table success"); //console.log("success");}//删除数据库中表内容的函数function deleteTable(table){ var db = html5rocks.webdb.db; db.transaction(function(tx){ tx.executeSql("DELETE FROM "+table, [],dbOnSuccess,dbOnError); }); }//判断数据库中是否已经插入数据 var db = html5rocks.webdb.db; db.transaction(function(tx) { tx.executeSql("SELECT * FROM aaa WHERE INFOID=? ", [id], function(tx, rs) { //枚举记录 if (rs.rows.length > 0) { for ( i = 0; i < rs.rows.length; i++) { var item = rs.rows.item(i); var bbb=item.INFOID; var ccc=item.TYPETAG; } } //执行插入数据的操作 if (rs.rows.length == 0) { tx.executeSql("INSERT INTO aaa(INFOID,TYPETAG) VALUES (?,?)", [id, type], function() { }, dbOnError); } },,dbOnError); });