HTML 5网络存储不能插入记录



在向存储中插入记录时遇到麻烦,调试了很多。似乎它从html表单抓取速率值,并将其传递给addItem函数。但是它不执行transaction.executeSql.

function errorHandler(transaction, error) {
    alert("SQL error: " + error.message);
}
try{
// open the database
var itemsDb = openDatabase('tripDb', '1.0', 'XB Trip DB', 100 * 1024);
// create the list items table if it doesn't exist
var queryCreateTable = 
    "CREATE TABLE IF NOT EXISTS xbTrip(xbId INTEGER NOT NULL PRIMARY KEY 
             AUTOINCREMENT, rate);";
itemsDb.transaction(function (transaction) {
    transaction.executeSql(queryCreateTable, [], null, errorHandler);
}, errorHandler);
}
catch (e)
{
alert(e.message);
}
$(document).ready(function() {
    $("#add").click(function() {
        addItem($("#rate").val(), function() {
        displayMessage();
    });     
        });
    function addItem(rate, callback) {      
    try{
        itemsDb.transaction(function(transaction) {
            transaction.executeSql(
                "INSERT INTO xbTrip(rate) VALUES (?)", 
                [rate],
                callback,
                errorHandler
            );
        });
    }
    catch (e)
    {
        alert(e);
    }       
} // addItem
function displayMessage(){
    alert("Insert record completed");
}
})  

我已经把你的代码扩展了一点。

你有一个或两个简单的语法错误,如分号在你的SQL字符串,不确定如果这只是在Soverflow帖子或不是。

这是你想要做的事情的一个工作实现:

http://jsfiddle.net/TnPPa/1/

也许把它和你的比较一下,看看你是否能告诉是什么导致你的代码在你的特定情况下出错,因为你没有提供HTML/App上下文。

我认为这可能只是语法问题,也许是匿名函数中一些过于复杂的回调传递。

让我知道你进展如何

最新更新