SQLResultSet对象
localStorage本地存储对象
该对象和手机设备上的存储没有关系,而是和html5中的本地存储提供的一个接口
一个完整的例子
html部分
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title> new document </title> </head> <body> <button onclick="pgapStorage()">pgapStorage</button> </body> <script type="text/javascript" src="cordova-2.1.0.js"></script> <script type="text/javascript" src="pgapstorage.js"></script> </html>
js部分
function pgapStorage(){ //创建或者打开数据库,大小100000 bytes var dbOne = window.openDatabase("myDataBase","1.0","phonegapdatabase",100000); //将事物对象SQLTransaction传给createATable函数,如果发生错误将SQLError对象传给errorCreateTable函数 dbOne.transaction(createATable, errorCreateTableTrans, successCreateTableTrans); } //执行回调函数createATable,创建表MyTab并且插入数据 function createATable(trans){ trans.executeSql('DROP TABLE IF EXISTS MyTab'); trans.executeSql('CREATE TABLE IF NOT EXISTS MyTab (id unique, data)'); trans.executeSql('INSERT INTO MyTab (id, data) VALUES (1, "First row")'); trans.executeSql('INSERT INTO MyTab (id, data) VALUES (2, "Second row")'); } //dbOne.transaction()执行失败后执行,并传递SQLError对象 function errorCreateTableTrans(err){ alert("err code:"+err.code+"err message:"+err.message); } //dbOne.transaction()执行成功后会执行 function successCreateTableTrans(){ alert("successfully"); var dbOne = window.openDatabase("myDataBase","1.0","phonegapdatabase",100000); //创建新的事物对象,并传递给回调函数 dbOne.transaction(queryMyTab,successQueryMyTabTrans,errorQueryMyTabTrans); } function successQueryMyTabTrans(){ alert("successQueryMyTabTrans"); } function errorQueryMyTabTrans(){ alert("err code:"+err.code+"err message:"+err.message); } //查询 function queryMyTab(trans){ //查询成功后执行successQueryMyTab回调函数,失败执行errorQueryMyTab函数 trans.executeSql('SELECT * FROM MyTab', [], successQueryMyTab, errorQueryMyTab); } //executeSql执行成功后执行,并将SQLTransaction、SQLResultSet两个对象传递给函数 successQueryMyTab function successQueryMyTab(trans,results){ var len = results.rows.length; alert(len); //利用弹出框显示查询的结果 for (var i=0; i<len; i++){ alert("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data); } } //executeSql()执行失败后执行 function errorQueryMyTab(err){ alert("err code:"+err.code+"err message:"+err.message); }