phonegap 中的存储(2)
console.log("MyTab table: " + len + " rows found.");
for (var i=0; i<len; i++){
console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data = " + results.rows.item(i).data);
}
}
localStorage本地存储对象
该对象和手机设备上的存储没有关系,而是和html5中的本地存储提供的一个接口
一个完整的例子
html部分
[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部分
[html]
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);
相关新闻>>
- 发表评论
-
- 最新评论 更多>>