开发者

html 5 web storage does not insert record

开发者 https://www.devze.com 2023-04-12 18:11 出处:网络
having trouble with inserting a record into storage, debugged quite a bit. seems it grabbed rate value from html form and passed it to the addItem function. but it does not execute transaction.execute

having trouble with inserting a record into storage, debugged quite a bit. seems it grabbed rate value from html form and passed it to the addItem function. but it does not execute 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) VA开发者_如何转开发LUES (?)", 
                [rate],
                callback,
                errorHandler
            );
        });
    }
    catch (e)
    {
        alert(e);
    }       
} // addItem

function displayMessage(){
    alert("Insert record completed");
}
})  


I've taken your code and expanded on it a little bit.

You had one or two simple syntax errors such as a semicolon in your SQL string, not sure if that was just in the Soverflow post or not.

Here's a working implementation of what you're trying to do:

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

Perhaps compare it with yours and see if you can tell what's causing your code to go wrong in your particular situation as you didn't provide the HTML/App context.

I think it was probably just syntax and perhaps some overcomplicated passing of callbacks within anonymous functions.

Let me know how you get on.

0

精彩评论

暂无评论...
验证码 换一张
取 消