3

我正在尝试为我的电话间隙应用程序设置一个数据库。问题是除了设置表的事务之外的所有事务都会产生错误说

“SQLTransactionCallback 为空或引发异常”

这是代码

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 成功时被拉取。会话页面启动时会拉取碎片数据。但是我可以用从会话表中提取的代码替换代码以拉出片段表,而不会出现任何问题。由于某种原因,似乎无法访问进入pieces 表的数据

4

2 回答 2

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);
}
于 2015-02-09T06:16:06.150 回答
0

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

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

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

即,您应该在成功回调“onSelectSessionSuccess”中选择片段,并且一切都会正常工作。

希望有帮助。

于 2013-09-13T08:43:30.957 回答