PhoneGap Sql与PhoneGap开发者App

时间:2015-02-19 23:30:25

标签: cordova phonegap-plugins

首先感谢阅读。

我有一个疑问,如果我使用phonegap开发者应用创建默认的phonegap应用程序(用我的手机连接无线以查看更改)然后我将sql的插件添加到我的config.xml并创建基本用于填充数据库并插入两行的代码。

如果我在w8中使用phonegapp开发者应用程序看到该应用程序应该可以处理数据库查询和那些事情吗?

因为现在似乎什么都行不通,我感到很困惑。我在config.xml中添加以下行来添加sql lite插件

<gap:plugin name="com.millerjames01.sqlite-plugin" version="1.0.1" />

这是我的index.html

<html>
<head>
    <meta charset="utf-8" />
    <meta name="format-detection" content="telephone=no" />
    <meta name="msapplication-tap-highlight" content="no" />
    <!-- WARNING: for iOS 7, remove the width=device-width and height=device-height attributes. See https://issues.apache.org/jira/browse/CB-4323 -->
    <meta name="viewport" content="user-scalable=no, initial-scale=1, maximum-scale=1, minimum-scale=1, width=device-width, height=device-height, target-densitydpi=device-dpi" />
    <link rel="stylesheet" type="text/css" href="css/index.css" />
    <title>Hello World</title>
</head>
<body>
    <div data-role="page">
      <div data-role="header" data-position="fixed" data-theme="b">
        <h1>Soccer Player</h1>
      </div>
      <div id="deviceready" data-role="content">
         <ul id="SoccerPlayerList">
        </ul>
      </div>
    </div>
    <script type="text/javascript" src="cordova.js"></script>
    <script type="text/javascript" src="js/index.js"></script>
    <script type="text/javascript">
        app.initialize();
    </script>
</body>
</html>

这是我的index.js:

var db = window.openDatabase("Dummy_DB", "1.0", "Just a Dummy DB", 200000); //will create database Dummy_DB or open it
var app = {

initialize: function() {
    this.bindEvents();
},

bindEvents: function() {
    document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
    app.receivedEvent('deviceready');
    db.transaction(populateDB, errorCB, successCB);
},

receivedEvent: function(id) {
    var parentElement = document.getElementById(id);
    var listeningElement = parentElement.querySelector('.listening');
    var receivedElement = parentElement.querySelector('.received');

    listeningElement.setAttribute('style', 'display:none;');
    receivedElement.setAttribute('style', 'display:block;');

    console.log('Received Event: ' + id);
}
};


//create table and insert some record
function populateDB(tx) {
    tx.executeSql('CREATE TABLE IF NOT EXISTS SoccerPlayer (id INTEGER PRIMARY KEY AUTOINCREMENT, Name TEXT NOT NULL, Club TEXT NOT NULL)');
    tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Alexandre Pato", "AC Milan")');
    tx.executeSql('INSERT INTO SoccerPlayer(Name,Club) VALUES ("Van Persie", "Arsenal")');
}

//function will be called when an error occurred
function errorCB(err) {
    alert("Error processing SQL: "+err.code);
}

//function will be called when process succeed
function successCB() {
    alert("success!");
    db.transaction(queryDB,errorCB);
}

//select all from SoccerPlayer
function queryDB(tx){
    tx.executeSql('SELECT * FROM SoccerPlayer',[],querySuccess,errorCB);
}

function querySuccess(tx,result){
    $('#SoccerPlayerList').empty();
    $.each(result.rows,function(index){
        var row = result.rows.item(index);
        $('#SoccerPlayerList').append('<li><a href="#"><h3 class="ui-li-heading">'+row['Name']+'</h3><p class="ui-li-desc">Club '+row['Club']+'</p></a></li>');
    });

    $('#SoccerPlayerList').listview();
}

1 个答案:

答案 0 :(得分:0)

问题是代码。 我解决它只是在我想要的操作系统中构建应用程序,然后在包含的.js中。我希望我的代码可以帮助某人开始使用移动电话固定应用程序=)。

var db = window.openDatabase("haberes", "1.0", "Haberes DB", 10000000);
var app = {
initialize: function() {
  this.bindEvents();
},

bindEvents: function() {
  document.addEventListener('deviceready', this.onDeviceReady, false);
},

onDeviceReady: function() {
  app.receivedEvent('deviceready');
},

receivedEvent: function(id) {
  db.transaction(populateDB, errorCB, successCB);
}
};

app.initialize();

function errorCB(tx, err) {
  alert("Error processing SQL: "+err);
};


function successCB(tx) {
    alert('Successfully Query');
};

function isTableExists(tx, tableName, callback) {
    tx.executeSql('SELECT * FROM '+tableName, [], function(tx, resultSet) {
      if (resultSet.rows.length <= 0) {
          callback(false);
      } else {
          callback(resultSet.rows.length);
      }
    }, function(err) {
      callback(false);
    })
};

function populateDB(tx) {

     isTableExists(tx, "perfil", function(status) {
              if (!status) {
                    tx.executeSql('CREATE TABLE IF NOT EXISTS perfil (id unique,prf_nombre,prf_apellido,prf_grado,prf_dni INT,prf_email)');
                    tx.executeSql('INSERT INTO perfil (id,prf_nombre,prf_apellido,prf_grado,prf_dni,prf_email) VALUES (1,"Gaston","Dedieu","Coronel",36791591,"gastondedieu@hotmail.com")');
              } else{
                alert('Filas en tabla perfil '+status);
              }
          });


        isTableExists(tx, "haber", function(status) {
            if (!status) {
                tx.executeSql('CREATE TABLE IF NOT EXISTS haber (id unique,hab_fecha,hab_neto REAL,hab_bruto REAL,hab_fecha_in TIMESTAMP,hab_prf_id)');
                tx.executeSql('INSERT INTO haber (id,hab_fecha,hab_neto,hab_bruto,hab_prf_id) VALUES (1,"24/02/2015",14256.65,19856.46,1)');
            } else{
                alert('Filas en tabla haber '+status);
            }
        });




      };