2012-03-02 42 views
1

我将数百行插入本地数据库。HTML5本地数据库回调

在此插入后,我想从该表中选择*来获取并显示所有这些记录。

我试着把Select语句放在INSERT事务的回调中,但是它不能正常工作。我测试了SELECT语句以查看它的工作原理,当我执行页面刷新时。

任何人都可以帮忙吗?插入所有记录后,我希望能够在不刷新页面的情况下全部选中它们。谢谢!

database.db.transaction(函数(TX){ JSON处理

   tx.executeSql('INSERT INTO ...'), 
        [DATA IS HERE], 

        function (tx, callback) { 
         alert(callback.message); 

         database.db.transaction(function (tx) { 
         tx.executeSql('SELECT * from table;', [], tableSelectHandler, errorHandler); 
             }); 
            }, 
        function (tx, error) { 
         alert(error.message); 
        } 

      }); 

回答

0

这是一个有点晚来回答这个特定问题,但可以帮助别人,所以......

的分贝.transaction函数也有Error和Success的回调函数,你可以将你的SELECT代码放在Success回调函数中,并且在执行完所有Insert之后,它会在事务结束时执行。基本上你应该有这样的东西:

database.db.transaction(function (tx) { //JSON PROCESSING 

    tx.executeSql('INSERT INTO ...'), 
     [DATA IS HERE], 
    function (tx, callback) { 
     alert("Insert ok"); 
    }, 
    function (tx, error) { 
     alert("Error inserting: " + error.message); 
    } 
}, 
function (error) { // This is the Error callback of the TRANSACTION 
    alert(error.message); 
}, 
function (result) { // This is the Success callback of the TRANSACTION 
    alert("TRANSACTION completed"); 

    database.db.transaction(function (tx) { 
     tx.executeSql('SELECT * from table;', [], tableSelectHandler, errorHandler); 
    }); // Your tableSelectHandler should take care of the result of the Select 
}); 

希望它有帮助。干杯!

+0

很高兴知道 - 我实际上休息一下,但很快就会再次参与其中。 – Garrett 2012-09-06 17:19:59