JSON数据插入sqLit数据库无法正常工作 [英] JSON data insert sqLit Database not working

查看:275
本文介绍了JSON数据插入sqLit数据库无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图 JSON 数据插入到 SqLit 数据库的PhoneGap 。我有两列创建表,像这样的:

I tried to JSON data insert into SqLit database in PhoneGap. I created a table with two columns, like this:

function setup(tx) {

    tx.executeSql('DROP TABLE IF EXISTS HEADER_DATA');
    tx.executeSql("create table if not exists bookinformation(inserkey TEXT, key TEXT)");
}

这code运行成功,并创建表。然后,我插入JSON数据到 bookinformation 表,是这样的:

This code runs successfully and the table is created. Then, I insert JSON data into the bookinformation table, like this:

function dbReady() {
db.transaction(function(tx) {
        alert("5");
        $.getJSON('http://echo.jsontest.com/key/value/one/two',function(data){
         $.each(data, function(i, dat){
         tx.executeSql('INSERT OR REPLACE INTO bookinformation (inserkey, key) VALUES("'+data.one+'", "'+data.key+'")');    
        alert("completed");
    });
});     
    }, errorHandler, function() { alert('added row'); });
}

然而,插入语句失败。我得到这个错误:

However, the insert statement fails. I get this error:

Uncaught InvalidStateError:Failed to execute 'executeSql' on 'SQLTransaction':SQL execution is disallowed

是什么原因造成这个错误?

What is causing this error?

推荐答案

老问题,但是这可能帮助别人。

这错误通常是由事务 TX 是陈旧所致。

That error is usually caused by the transaction tx being stale.

这是因为Ajax调用和的时候你的Ajax回调被击中了 TX 对象不再有效。如果你用同样的情况,的setTimeout 或费时非Websql操作以及。

This is because of the ajax call and by the time your ajax callback gets hit that tx object is no longer valid. The same happens if you use setTimeout or any time consuming non-Websql operation aswell.

要避免简单地说,在回调内创建事务。

To avoid that simply, create the transaction inside in the callback.

例如

function dbReady() {
    $.getJSON('http://echo.jsontest.com/key/value/one/two',function(data) {
        db.transaction(function(tx) {
            alert("5");
            $.each(data, function(i, dat) {
                tx.executeSql('INSERT OR REPLACE INTO bookinformation (inserkey, key) VALUES("'+data.one+'", "'+data.key+'")');
            });
            alert("completed");
        }, errorHandler, function() { alert('added row'); });
    });
}

这篇关于JSON数据插入sqLit数据库无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆