0

这是我用来在基于 phonegap 2.9.0 的应用程序中存储和初始化数据库的代码

 var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);
 db.transaction(populateDB, errorCB, successCB);

 function populateDB(tx)
 {
   tx.executeSql('DROP TABLE IF EXISTS DEMO');
   tx.executeSql('CREATE TABLE IF NOT EXISTS DEMO (id unique, data)');
   tx.executeSql('INSERT INTO DEMO (id, data) VALUES (1, "First row")');
   tx.executeSql('INSERT INTO DEMO (id, data) VALUES (2, "Second row")');
 }

  function successCB()
  { 
  alert("Sucesss")
  var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);            
  db.transaction(queryDB, errorCBs);
  }

  function queryDB(tx) 
  {
  tx.executeSql('SELECT * FROM DEMO', [],errorCB, querySuccess);
  }

  function querySuccess(tx, results)
  {
  alert("Insert ID = ",results.insertId);
  alert("Rows Affected = ",results.rowAffected);
  alert("Insert ID = ",results.rows.length);
  }
  function errorCBs(err)
  {
   alert("2nd error ",err.code);
  }
  function errorCB(err)
  {
  alert("Error processing SQL: ",err.code);
  }

当应用程序调用

 db.transaction(populateDB, errorCB, successCB);

它首先显示successCB的警报,然后它显示警报errorCB。我的代码有什么问题。请帮我解决

4

1 回答 1

0

您不需要打开数据库两次。

取出部分:

var db = window.openDatabase("Database", "1.0", "PhoneGap Demo", 200000);            

successCB功能上。

于 2014-08-15T22:49:49.313 回答