Phonegap本地数据库不会显示输入的数据

时间:2015-05-20 04:41:34

标签: javascript android cordova phonegap-plugins phonegap-build

我正在使用phonegap和cordova(这是在Android工作室设置的)我无法将结果输入数据库进行显示。

我将收到以下 Console.logs 以显示:

  

In device ready | Tables created | Records inserted into Team & Player

我不太清楚为什么数据库不会显示输入的结果?事实上 alert()功能中的任何console.log()showRecords() db.transaction(function(trans){。我试过更改代码并更改SQL语句,但我真的不确定问题是什么。

  

我在chrome上使用Ripple模拟器来查看应用程序   | |以前的应用程序使用数据库事务功能。

我的main.js文件

var db = null;


var createTblTeam = "CREATE TABLE IF NOT EXISTS tblTeam (fldTeamId INTEGER PRIMARY KEY, fldTeamName TEXT);";
var createTblPlayer = "CREATE TABLE IF NOT EXISTS tblPlayer (fldPlayerId INTEGER PRIMARY KEY, fldPlayerLastname TEXT, fldPlayerFirstname TEXT, fldDOB INTEGER, fldJumperNumber INTEGER, fldTeamID FOREIGN KEY);";

var insertTeam = "INSERT INTO tblTeam (fldTeamId, fldTeamName) VALUES (?,?);";
var insertPlayer = "INSERT INTO tblPlayer (fldPlayerId,fldPlayerLastname,fldPlayerFirstname ,fldDOB ,fldJumperNumber ,fldTeamID) VALUES (?,?,?,?,?,?);";



var showTeam = "SELECT * FROM tblTeam;";
var showPlayer = "SELECT * FROM tblPlayer;";


// var deleteAllRecsSQL = "DELETE FROM tblFooty;";
// var updateSQL = "UPDATE tblFooty SET fldFirstname = ?, fldLastname = ?, fldJumper = ? WHERE fldId = ?;";
// var deleteRecSQL = "DELETE FROM tblFooty WHERE fldId = ?;";
// var findRecordSQL = "SELECT * FROM tblFooty WHERE fldId = ?;";

document.addEventListener("deviceready", onDeviceReady, false);

function onDeviceReady()
{
  //alert("In deviceReady");
  console.log("In deviceReady");

  try
  {
    if( !window.openDatabase() )
    {
      alert("Databases on this device is not supported!");
    }
    else
    { 

      // this will open the database regardless of version
      var version = "";
      var displayName = "Footy Players";
      var maxSize = 1000;

      db = window.openDatabase("Team", version, "Team Details", maxSize);
      db = window.openDatabase("Player", version, "Player Details", maxSize);

      db.transaction(populateDB, onError, showRecords);
    }
  }
  catch(err)
  {
    if(err == 2)
      alert("Invalid database version");
    else
      alert("Unknown error: " + err);
  }
}

function populateDB(trans)
{
  db.transaction(function (trans){

    trans.executeSql(createTblTeam, []);
    //Insert Into TEAM DB
    trans.executeSql(insertTeam, [1,"Melbourne"]);

    trans.executeSql(createTblPlayer, []);
     //Insert Into Player DB
    trans.executeSql(insertPlayer, [1,"Smith","James" ,15051998 ,2]);

    console.log("Tables created");


    console.log("Records inserted into Team & Player");   

  });
}

function showRecords()
{
  db.transaction(function(trans){
    trans.executeSql(showTeam, [], function(trans, result){
      var str = "";
      alert(row['fldTeamId'] + " " + row['fldTeamName']);
      if(result.rows.length == 0)
      {
        str += "<em>There are no records to display!<em>";
      }
      else
      {
        str += "<table border='0' cellpadding='0' width='80%'>";

        for( var i = 0, row = null ; i < result.rows.length ; i++)
        {
          row = result.rows.item(i);

          //alert(row['fldId']);
          //str += "<tr><td>" + row['fldId'];

          str += "</td><td>" + row['fldTeamId'];
          str += "</td><td>" + row['fldTeamName'];
          alert('hi');
          str += "</td></tr>"
        }
        str += "</table>";
      }

      $("#results").html(str);

    });
  });
}
function onError(err)
{
  console.log(err.code);

  switch(err.code)
  {
    case 0  : console.log("Non database error: " + err.message);
          break;
    case 1  :   console.log("Some database error: " + err.message);
          break;
    case 2  :   console.log("Invalid database version: " + err.message);
          break;
    case 3  : console.log("Dataset too large to return from query: " + err.message);
          break
    case 4  :   console.log("Stoprage limit exceeded: " + err.message);
          break;
    case 5  : console.log("Lock contention error: " + err.message);
          break;
    case 6  : console.log("Constraint failure: " + err.message);
          break;
    default : console.log("Error: " + er.code + " "  + err.message);
  } 
}

Index.html(jQuery Mobile)

<div data-role="page" id="page">
    <div data-role="header">
        <h1>Page One</h1>
    </div>
    <div data-role="content">   
        <ul data-role="listview">
            <li><a onClick="showRecords();" href="#page1">List Players</a></li>
            <li><a href="#page2">Add Player</a></li>
            <li><a href="#page3">Search for player</a></li>
            <li><a href="#page4">Delete a player</a></li>
        </ul>       
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>


<div data-role="page" id="page1">
    <div data-role="header">
        <h1>List Players</h1>
    </div>
    <div data-role="content">   
        <p id="results"></p>
    </div>
    <div data-role="footer">
        <h4>Page Footer</h4>
    </div>
</div>

1 个答案:

答案 0 :(得分:0)

您是否为cordova安装了sqlite插件?为了正确使用数据库,您必须安装SQL lite插件。这是链接(https://github.com/chbrody/Cordova-SQLitePlugin/