|
- <!DOCTYPE HTML>
- <html>
- <head>
- <meta charset="utf-8">
- <title>PhoneGap-Storage</title>
- <link rel="stylesheet" href="../jquery.mobile-1.1.1/jquery.mobile-1.1.1.css" />
- <script src="../jquery.mobile-1.1.1/jquery.js"></script>
- <script src="../jquery.mobile-1.1.1/jquery.mobile-1.1.1.js"></script>
- <script type="text/javascript" charset="utf-8" src="../cordova-1.9.0.js"></script>
- <script type="text/javascript">
- $(document).ready(function(){
- document.addEventListener("deviceready", myDeviceReadyListener, false);
- });
-
- function myDeviceReadyListener(){
- var database = window.openDatabase("BeifengPhoneGap", "1.1", "PhoneGapDB", 102400);
- createTable(database);
- queryTable(database);
- }
-
- createTable = function(db){
- db.transaction(function(tx){
- tx.executeSql('CREATE TABLE IF NOT EXISTS tbl_user (id int PRIMARY KEY, user_name NVARCHAR(255),password NVARCHAR(255))');
- },function(err){
- console.log("执行失败"+err);
- },function(){
- console.log("执行成功!");
- });
- }
-
- insertTable = function(db){
- db.transaction(function(tx){
- tx.executeSql('INSERT INTO tbl_user VALUES(?,?,?)',[7,"beifeng","beifeng"],function(tx,resultset){
- if(resultset.rowsAffected) {
- console.log("插入数据对表有影响!");
- }
- console.log("rowAffected:" + resultset.rowsAffected);
- console.log("insertId:" + resultset.insertId);
- },function(err){
- console.log("插入数据失败:"+err.code);
- });
- },function(err){
- console.log("执行失败"+err.code);
- },function(){
- console.log("执行成功!");
- })
- }
-
- queryTable = function(db){
- db.transaction(function(tx){
- tx.executeSql('SELECT * from tbl_user',[],function(tx,resultset){
- if(resultset.rowsAffected) {
- console.log("插入数据对表有影响!");
- }
- console.log("rowsAffected:" + resultset.rowsAffected);
- console.log("rows:" + resultset.rows.length);
- for(var i = 0; i < resultset.rows.length;i++){
- console.log(resultset.rows.item(i).id);
- }
- },function(err){
- console.log("插入数据失败:"+err.code);
- });
- },function(err){
- console.log("执行失败"+err.code);
- },function(){
- console.log("执行成功!");
- })
- }
-
- </script>
- </head>
- <body>
- <div data-role="page">
- <div data-role="header">
- <h1>Storage</h1>
- </div>
- <div data-role="content">
- </div>
- <div data-role="footer">
- <h4> </h4>
- </div>
- </div>
- </body>
- </html>
复制代码 一个完整的例子
|
|