2012-03-02 27 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