Write multiple row at a time

偶尔善良 提交于 2019-12-24 21:43:31

问题


I am using ionic v1 and angularjs and add value into database by using foreach loop but its getting slow to me(500 times execute this loop).

Factory 1:

self.query = function(query, parameters) {
    return $cordovaSQLite.execute(db, query, parameters).then(function(result) {
       //console.log(result);
        return result;
    }, function (error) {
        console.log(error);
        return error;
    });
};

Factory 2:

self.add = function (url,data) {
  return DBA.query("INSERT OR REPLACE INTO offline_data (id, data) VALUES (?, ?)", [url, JSON.stringify(data)]);
};

adding db via:

   angular.forEach(result.style_list, function(value){
              OfflineDataService.add(value.style_id, value)
   });

how we can faster....


回答1:


Sounds like you need to find a way to do a bulk insert, ie execute many inserts in a single SQL statement:

INSERT OR REPLACE INTO offline_data (id, data) 
    VALUES (?, ?), (?, ?), (?, ?), ...

This will depend on your DB and what it supports, but if it does then you can just batch up your data to insert into say batches of 50 rows at a time.



来源:https://stackoverflow.com/questions/48396222/write-multiple-row-at-a-time

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!