HTML5 WEB SQL的实践

widuu 2013-3-19 前台技术 5,183 0 喜欢 (0)

html5支持web sql database这能做成本地化交互的一种操作,还是时间没那么多 只放出代码给大家参考
[html]
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Web SQL</title>
</head>
<body>
<script>
var db=false;
var dbo={
createDatabase:function(dbName,ver,descripe,size){
db=window.openDatabase(dbName,ver,descripe,size);
},
executeSQL:function(sql){
db.transaction(function(fx){
fx.executeSql(sql);
});
},
query:function(sql,recordset){
db.transaction(function(fx){
fx.executeSql(sql,[],function(a,result){recordset(result);},function(){recordset("error");}
);
});
}
};
dbo.createDatabase("testDB","1.0","测试数据库",2000);
dbo.executeSQL("create table xxx (id int primary key, user char(8) null)");
dbo.executeSQL("insert into xxx (id , user) values(1, 'aaaa');");
dbo.executeSQL("insert into xxx (id , user) values(2, 'bbbb');");
dbo.executeSQL("insert into xxx (id , user) values(3, 'cccc');");
dbo.executeSQL("insert into xxx (id , user) values(4, '测试中文');");

dbo.query("select * from xxx",function(myrecord){
for(var i=0;i<myrecord.rows.length;i++){
console.log( myrecord.rows.item(i)["id"]+"-"+myrecord.rows.item(i)["user"] );
}
});

dbo.query("select max(id) as maxId from xxx",function(maxId){

console.log( "最大ID:"+maxId.rows.item(0)["maxId"] );
});

</script>
</body>
</html>
[/html]

转载请注明来自微度网络,本文标题:《HTML5 WEB SQL的实践》

喜欢 0 发布评论
Top