码迷,mamicode.com
首页 > 数据库 > 详细

使用phonegap操作数据库

时间:2017-03-29 22:41:11      阅读:246      评论:0      收藏:0      [点我收藏+]

标签:int   als   rdo   process   eve   ready   title   success   exists   

<!DOCTYPE html>
<html>

	<head>
		<meta charset="UTF-8">
		<title>Database Example</title>

		<script type="text/javascript" charset="UTF-8" src="cordova.js"></script>
		<script type="text/javascript" charset="UTF-8">
			document.addEventListener("deviceready", onDeviceReady, false);

			function onDeviceReady() {
				var db=window.openDatabase("Test","1.0","Test",200000);
				db.transaction(populateDB,errorDB,successDB);
			}
			
			//初始化数据库数据
			function populateDB(tx){
				tx.executeSql(‘DROP TABLE IF EXISTS test1‘);
				tx.executeSql(‘CREATE TABLE IF NOT EXISTS test1 (id unique,name)‘);
			    tx.executeSql(‘INSERT INTO test1(id,name) VALUES(1,"Tony")‘);
				tx.executeSql(‘INSERT INTO test1(id,name) VALUES(2,"Bill")‘);
				tx.executeSql(‘INSERT INTO test1(id,name) VALUES(3,"Tony")‘);
			}
			
			//查询方法
			function queryDB(tx){
				tx.executeSql(‘SELECT * FROM test1‘,[],querySuccess,errorDB);
			}
			
			//查询成功回调
			function querySuccess(tx,results){
				var len=results.rows.length;
				var status=document.getElementById("status");
				var string ="Rows:"+len+"<br />";
				for(var i=0;i<len;i++){
					string+=results.rows.item(i).name+"<br />";
				}
				status.innerHTML=string;
			}
			
			//执行初始化数据成功后的回调
			function successDB(){
				var db=window.openDatabase("Test","1.0","Test",200000);
				db.transaction(queryDB,errorDB);
			}
			
			//失败回调
			function errorDB(err){
				alert(‘Error processing SQL:‘+err.code);
			}
			
		</script>
	</head>

	<body>
		<h1>Names</h1>
		<div id=‘status‘></div>
	</body>

</html>

  

使用phonegap操作数据库

标签:int   als   rdo   process   eve   ready   title   success   exists   

原文地址:http://www.cnblogs.com/ganchuanpu/p/6641878.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!