标签:http io os ar java for sp 数据 on
1.新建数据库
DEMODB = openDatabase( shortName, //数据库名,浏览器和SQL语句中会用到 version, //openDatabase 的版本,用1.0就可以了 displayName, //数据库显示的全名/描述 maxSize //数据库期望的上限体积大小 );
2.执行方法
DEMODB.transaction( function (transaction) { transaction.executeSql(sql, [参数]argument, 回调函数callback, 错误处理errorCallback); } );
3.处理返回集
function callback(transaction, results){ for (var i=0; i<results.rows.length; i++) { var row = results.rows.item(i);//row为每一行 xx=row[‘colName‘];//colName为字段名 } }
demo:
1.初始化数据库后,新建一张表添加默认数据。
/** *初始化数据库 */ function initDatabase() { try { if (!window.openDatabase) {//先检测一下浏览器是否支持这个方法,如果不支持进行提示 alert(‘Databases are not supported in this browser.‘); } else {否则创建数据库 var shortName = ‘DEMODB‘; var version = ‘1.0‘; var displayName = ‘DEMO Database‘; var maxSize = 100000; // bytes DEMODB = openDatabase(shortName, version, displayName, maxSize); createTables();//创建表结构 selectAll();//查询所有值 } } catch(e) {//异常处理 if (e == 2) { // Version number mismatch. console.log("Invalid database version."); } else { console.log("Unknown error "+e+"."); } return; } }
function createTables(){ DEMODB.transaction( function (transaction) { transaction.executeSql(‘CREATE TABLE IF NOT EXISTS demodb(id INTEGER NOT NULL PRIMARY KEY, name TEXT NOT NULL,password TEXT NOT NULL);‘, [], nullDataHandler, errorHandler); } ); insertData();//添加数据 } //添加数据 function insertData(){ DEMODB.transaction( function (transaction) { var data = [‘1‘,‘hello‘,‘word‘]; transaction.executeSql("INSERT INTO demo(id,name, password) VALUES (?, ?, ?, ?, ?)" , [data[0], data[1], data[2]); } ); }
2.查询数据
function selectAll(){ DEMODB.transaction( function (transaction) { transaction.executeSql("SELECT * FROM demodb;", [],callbackHandler, errorHandler); } ); } function callbackHandler(transaction, results){ for (var i=0; i<results.rows.length; i++) { var row = results.rows.item(i); var newFeature = new Object(); name = row[‘name‘]; password = row[‘password‘]; document.getElementById("test").value=name; } }
参考自:http://www.oschina.net/question/12_26204
标签:http io os ar java for sp 数据 on
原文地址:http://my.oschina.net/u/2255569/blog/338369