【问题标题】:How to import JSON into SQLite with jquery如何使用 jquery 将 JSON 导入 SQLite
【发布时间】:2012-09-21 02:26:59
【问题描述】:

我已经用 HTML5 和 JS (phonegap) 开发了一个应用程序,现在我必须将 JSON 导入本地 SQLite。 我有以下代码以便从外部服务器导入

        $.ajax({
        url: 'path_to_my_file',
        data: {name: 'Chad'},
        dataType: 'jsonp',
        success: function(data){
                var count = data.posts.length;
                $.each(data.posts, function(i,post){
                    notesdb.transaction(function(t) {
                    t.executeSql('INSERT into bill (barcode, buildingcode, buildingaddress, flatname, flatdescription, entryseason, period, amount, pastpayments, todaypayments, receiptno) VALUES (?,?,?,?,?,?,?,?,?,?,?);',
                        [post.Id, post.Code, post.Address, post.Name, post.Description, post.EntrySeason, post.Period, post.Revenue, post.PastPayments, post.todaypayments, post.receiptno],
                        function(){ 
                            bill = bill + 1;
                            $('#mycontent article').html(bill + '/' + data.posts.length + ' <strong>bill</strong>');

                            if (--count == 0) {
                                $('#mycontent article').html('<strong>bill - <font color=green>OK</font></strong>');
                            }
                        }
                    );
                    });
                });
            }
    });

例如,如果我必须导入本地 json var,我应该如何转换代码

var mybilljson = jQuery.parseJSON( billjson );

进入同一个 SQLite?

【问题讨论】:

    标签: jquery json sqlite


    【解决方案1】:

    从服务器接收到数据后,将调用您的 success 函数。
    posts 中的每个元素调用传递给 each 的函数。
    事务开始时调用传递给transaction 的函数。
    传递给executeSql 的函数在命令执行完毕后被调用。

    您没有服务器请求,我假设mybilljson 包含一张账单,因此您必须放弃前两张。结果会是这样的:

    var mybill = ...;
    notesdb.transaction(function(t) {
        t.executeSql('INSERT INTO bill(...) VALUES(...)',
                     [mybill.Id, ...],
                     function() {
                         // whatever
                     });
    });
    

    【讨论】:

      猜你喜欢
      • 2011-05-18
      • 2011-07-26
      • 1970-01-01
      • 1970-01-01
      • 2018-03-19
      • 2015-08-22
      • 2022-01-11
      • 2023-03-13
      • 1970-01-01
      相关资源
      最近更新 更多