Android SQ Lite - 如何有效地一次插入超过1000行?

时间:2017-07-13 09:49:13

标签: android sqlite ionic-framework sql-insert large-data

我正在使用Ionic Framework。我想请求帮助,一次插入超过1000行,同时插入向用户显示加载微调器,以便数据库中不会有任何混乱。

首先,我有两个服务/工厂。

数据库:

.factory('DB', function ($ionicPopup, $cordovaSQLite, $q, $ionicPlatform, $cordovaNetwork,$ionicLoading) {
        var self = this;
        self.query = function (query, parameters) {
            parameters = parameters || [];
            var q = $q.defer();
            $ionicPlatform.ready(function () {
                $cordovaSQLite.execute(db, query, parameters)
                    .then(function (result) {
                        console.log(result);
                        q.resolve(result);
                    }, function (error) {
                        console.log(error+"  .."+error.message);
                        alert('I found an error' + error.message);
                        q.reject(error);
                    });
            });
            return q.promise;
        }
        // Proces a result set
        self.getAll = function (result) {
            var output = [];

            for (var i = 0; i < result.rows.length; i++) {
                output.push(result.rows.item(i));
            }
            return output;
        }
        // Proces a single result
        self.getById = function (result) {
            var output = null;
            output = angular.copy(result.rows.item(0));
            return output;
        }
        return self;
    })

其次,AsyncService用于从多个URL下载数据

.service('asyncService', function ($http, $q) {
        return {
            loadDataFromUrls: function (urls) {
                alert("I am inside new Service ");
                var deferred = $q.defer();
                var urlCalls = [];
                angular.forEach(urls, function (url) {
                    urlCalls.push($http.get(url.url));
                });
                // they may, in fact, all be done, but this
                // executes the callbacks in then, once they are
                // completely finished.
                $q.all(urlCalls)
                    .then(
                    function (results) {
                        deferred.resolve(results)
                    },
                    function (errors) {
                        console.log(errors);
                        deferred.reject(errors);
                    },
                    function (updates) {
                        console.log(updates);
                        deferred.update(updates);
                    });
                return deferred.promise;
            }
        };
    })

首先,应该下载数据,然后将它们插入到属于的表中。

asyncService.loadDataFromUrls(urLs).then(function (result) {
                DB.query("DELETE FROM INV");
                // when i have to update the tables, i first delete them and then fill them back again.
                $ionicLoading.show({
                    template : 'Processing into Database. Please Wait...',
                    timeout : '6000'                      
                });
                result.forEach(function (rows) {
                    console.log(rows.config.url);
                    var i = 0;
                    if (rows.config.url == 'http://11.444.222.55:55/mobil_op?op=get_inv') {
                        rows.data.forEach(function (entry) {
                            var parameters = [entry.code, entry.name,];
                            DB.query("INSERT INTO INV (CODE,NAME,......) VALUES(?,?........)",parameters);
                        })
                    }
                })

            }, function (err) {
                alert("OOpsss ! : " + err.message);
                console.log("error");
            }, function (updates) {
                alert("updates");
                console.log("updates" + updates);
            })

将4453个元素插入数组时应该如何工作?

1 个答案:

答案 0 :(得分:1)

&#13;
&#13;
ByCSSSelector
&#13;
&#13;
&#13;