Phonegap数据库错误

时间:2013-09-12 11:07:26

标签: sql cordova

我正在尝试为我的手机间隙应用程序设置数据库。问题是除了设置表的那些事务之外的所有事务产生错误说

“SQLTransactionCallback为null或引发异常”

这是代码

function Datasetup()
{
   db=window.openDatabase("PracticeData","1.0","saveData",300000);
   alert("1");
   db.transaction(getDB,onDBError,onDBSuccess);
}
function onDBError(error)
{
    alert("Database Error"+error.message);
}

function onDBSuccess(tx,results)
{
    alert("successfull");
}

function getDB(tx)
{
   alert("2");
   tx.executeSql("CREATE TABLE IF NOT EXISTS session(date,length,activity,pieces)");
   alert("3");
   tx.executeSql("CREATE TABLE IF NOT EXISTS                         pieces(newpiece,name,composer,youtube,images_src,date_Added)");
   alert("4");
   tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError());
   tx.executeSql("SELECT * FROM session",[],onSelectSessionSuccess,onDBError());
   alert("5");
   tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());
 }

 function savepiece(tx)
 {
    tx.executeSql("INSERT INTO          NOTES(newpiece,name,composer,youtube,images_src,date_Added)VALUES(?,?,?,?,?,?)",[true,pieceData.name,pieceData.composer,"tube","images",date()]);
 }

我没有得到这个或创建表的错误信息

       db.transaction(getDB,onDBError,onDBSuccess)

我收到了

的错误消息
        tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError());

        tx.executeSql("SELECT * FROM session",[],onSelectSessionSuccess,onDBError());
         alert("5");
        tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());

这是一个很好的帮助,现在我的一个选择工作另一个但是提出

“语句回调引发异常或语句错误回调没有返回false”

这是sql无法正常工作

     tx.executeSql('SELECT * FROM session', [],onSelectPiecesSuccess, onDBError);

这是更新的代码

    function Datasetup(){

db=window.openDatabase("PracticeData","1.0","PracticeData",300000);
db.transaction(getDB,onDBError,onDBSuccess);
    }

    function onDBError(error){
alert("Database Error "+error.message);
    }
            function onDBSuccess(tx,results){
        //db.transaction(query,onDBError);
db.transaction(query,onDBError);
        alert("before");

    }


    function getDB(tx){
//alert("dropping")
//tx.executeSql("DROP TABLE pieces");

tx.executeSql("CREATE TABLE IF NOT EXISTS session(date,length,activity,pieces)");
tx.executeSql("CREATE TABLE IF NOT EXISTS pieces(newpiece,name,composer,youtube,images_src,date_added)");
//tx.executeSql('INSERT INTO session(date, length, activity,pieces) VALUES ("10-2-12", "15","2","11")');
//tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("11-2-12", "15","2","11")');
//tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("12-2-12", "15","2","11")');*/
tx.executeSql('INSERT INTO session (date, length, activity,pieces) VALUES ("13-2-12", "15","2","violin")');
tx.executeSql('INSERT INTO pieces (newpiece, name, youtube,images_src,date_Added) VALUES ("true", "15","tube","11",13-9-13)');
//tx.executeSql("DROP TABLE pieces");
//tx.executeSql("DROP TABLE session");
//alert("vi");
    }
            /**sessions**/
            function query(tx){ 
tx.executeSql('SELECT * FROM session', [], onSelectSessionSuccess, onDBError);
    }
    function onSelectSessionSuccess(tx,results){
dbResult = results;
var len= results.rows.length;
var sessionList="";

for(var i=0;i<len;i++)
{
    sessionList = sessionList+"<li>"+results.rows.item(i).date+"</li>"
}
alert(sessionList);
//tx.executeSql("SELECT * FROM pieces",[],onSelectPiecesSuccess,onDBError());
db.transaction(piecesquery,onDBError);
    }

    /**pieces**/
            function piecesquery(tx){
//alert("piecesquery");
tx.executeSql('SELECT * FROM session', [],onSelectPiecesSuccess, onDBError);
    }
    function onSelectPiecesSuccess(tx,results){
var len= results.rows.length;
var PiecesList="";
var newPiecesList="";
var res;
alert(len);
for(var i=0;i<len;i++)
{
    newPiecesList=newPiecesList+"<li>"+results.rows.item(i).newpiece + results.rows.item(i).composer +"</li>"
}
alert(newPiecesList);
$('#newPiecesList').innerHTML(newPiecesList);

    }

我的流程  这些表是创建并填充的getDB。 会话数据取决于getDb的成功。 会话页面启动时会拉出各个数据。但我可以替换代码来拉取片段表与代码从会话表中拉出没有问题。 由于某种原因,似乎无法访问进入件表的数据

2 个答案:

答案 0 :(得分:1)

我已经完成了表创建,插入和选择过程。希望它有用。

var db=null;
document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {

    db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');

    alert("db1");
    db.transaction(populateDatabase,errorDb,successDb);


}
 function populateDatabase(tx){

    tx.executeSql('DROP TABLE IF EXISTS TestTable');
    tx.executeSql('CREATE TABLE IF NOT EXISTS TestTable(RollNo INT PRIMARY KEY,FirstName text,LastName text,MobileNo text)');

    tx.executeSql('INSERT INTO TESTTABLE (RollNo, FirstName, LastName, MobileNo) VALUES (1, "Nisari", "Balakrishnan", "8891924207")');
    tx.executeSql('INSERT INTO TESTTABLE (RollNo, FirstName, LastName, MobileNo) VALUES (2, "Mikhil", "Anandan", "9605432101")');

}
function queryDB(tx) {
  tx.executeSql('SELECT * FROM TESTTABLE', [], querySuccess, errorCB);
}
function querySuccess(tx, results) {
    var len = results.rows.length;
    alert(len);
    console.log("DEMO table: " + len + " rows found.");
    for (var i=0; i<len; i++){
        //console.log("Row = " + i + " ID = " + results.rows.item(i).id + " Data =  " + results.rows.item(i).data);
        alert("Row = " + i + " ID = " + results.rows.item(i).RollNo + " FirstName =  " + results.rows.item(i).FirstName + " LastName =  " + results.rows.item(i).LastName + " MobileNo =  " + results.rows.item(i).MobileNo);

//            db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');
//            db.transaction(updateDB, errorCB);
//            console.log("After Open DB");


       }
    }
   function errorCB(err) {
console.log("Error processing SQL: "+err.code);
 }



function errorDb()
{
    alert("Error on Database creation:" + Error);
}
function successDb()
{
    alert("Database is created successfully");
          db = window.openDatabase("TestDatabase","1.0","TestingDatabase",'200000000');
    db.transaction(queryDB, errorCB);
}

答案 1 :(得分:0)

用于SELECT查询的executeSql是一个异步命令。 'onSelectSessionSuccess'是回调函数,您将在该函数中从表中接收结果。

由于这些是异步调用,因此执行不会等待查询返回数据。

将下一个SELECT调用作为上一次调用成功回调的一部分。

即你应该在成功回调'onSelectSessionSuccess'中选择一些东西,事情就可以了。

希望有所帮助。