【问题标题】:Adding rows to a sqlite database using a loop (Phonegap)使用循环将行添加到 sqlite 数据库(Phonegap)
【发布时间】:2012-11-12 13:04:11
【问题描述】:

我创建了一个小的 Phonegap 应用程序,它通过 XML 从 Ajax 调用中获取新闻数据。这很好用,但我想将数据存储在数据库表中,以便离线阅读新闻。

所以当 Ajax 回调循环遍历数据时,我用它填充一个全局新闻对象,然后调用一个函数来检查数据是否已经存储在数据库中。如果不是,则应将其插入数据库新闻表中。

问题是在将新闻存储在表中的事务中,似乎我的新闻对象不再存在,因为我收到了消息:

Uncaught TypeError: Cannot read property 'title' of undefined in ...

那么我怎样才能确保这个工作呢?这是我选择新闻并想检查它是否已经存在的部分的代码:

//  Check if a news from the internet already exists in the database; If not, insert it
function checkNewsInDB(){
    db.transaction(function(tx){
        tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB);
    }, dbErrorCB, dbSuccessCB);
}

//  Result Callback from the News Check
function checkSuccess(ctx, result){
    var len = result.rows.length;
    var found = false;
    for(var n = 0; n < newsContainer.length; n++){
        for(var r = 0; r < len; r++){
            if(result.rows.item(r).n_title == newsContainer[n].title 
               && result.rows.item(r).n_pubdate == newsContainer[n].pubdate){
                found = r;
            }
        }
        if(found == false){
            db.transaction(function(tx){
                tx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)", [newsContainer[n].title, newsContainer[n].link, newsContainer[n].creator, newsContainer[n].pubdate, newsContainer[n].description], insertSuccess, dbErrorCB);
            }, dbErrorCB, dbSuccessCB);
        } else {
            found = false;
        }
    }
}

newsContainer 填充了几行数据,我已经检查过了。如果有人能帮助我理解为什么这不起作用,我会很高兴。

提前致谢!

您好,

伯纳德

【问题讨论】:

    标签: javascript android sqlite cordova


    【解决方案1】:

    db.transaction 是异步的 - 到 executeSql 实际运行时,n 已经递增到循环结束。

    不要为每个项目创建一个新事务,而是尝试在事务函数中移动循环。

    【讨论】:

    • 能否发个示例代码,我还是搞不懂
    【解决方案2】:

    感谢您的回答。以下代码适用于所有有相同问题的人:

    //  Check if a news from the internet already exists in the database; If not, insert it
    function checkNewsInDB(){
        db.transaction(function(tx){
            tx.executeSql("SELECT * FROM NEWS", [], checkSuccess, dbErrorCB);
        }, dbErrorCB, dbSuccessCB);
    }
    
    //  Result Callback from the News Check
    function checkSuccess(ctx, result){
        var len = result.rows.length;
        var found = false;
        for(var n = 0; n < newsContainer.length; n++){
            for(var r = 0; r < len; r++){
                if(result.rows.item(r).n_title == newsContainer[n].title 
                   && result.rows.item(r).n_pubdate == newsContainer[n].pubdate){
                    found = r;
                }
            }
            if(found == false){
                var title = newsContainer[n].title;
                var link = newsContainer[n].link;
                var creator = newsContainer[n].creator;
                var pubdate = newsContainer[n].pubdate;
                var description = newsContainer[n].description;
                ctx.executeSql("INSERT INTO NEWS (n_title, n_link, n_creator, n_pubdate, n_description) VALUES (?,?,?,?,?)",
                            [title, link, creator, pubdate, description], insertSuccess, dbErrorCB);
            } else {
                found = false;
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-01-26
      • 1970-01-01
      • 1970-01-01
      • 2018-11-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多