Android SQ Lite-如何高效地一次插入1000多行?
我使用的是离子框架。我想寻求帮助,了解如何一次插入1000多行,同时向用户显示加载微调器,这样数据库中就不会有任何混乱 首先,我有两个服务/工厂 数据库:Android SQ Lite-如何高效地一次插入1000多行?,android,sqlite,ionic-framework,sql-insert,large-data,Android,Sqlite,Ionic Framework,Sql Insert,Large Data,我使用的是离子框架。我想寻求帮助,了解如何一次插入1000多行,同时向用户显示加载微调器,这样数据库中就不会有任何混乱 首先,我有两个服务/工厂 数据库: .factory('DB', function ($ionicPopup, $cordovaSQLite, $q, $ionicPlatform, $cordovaNetwork,$ionicLoading) { var self = this; self.query = function (query, pa
.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.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个元素时应该如何工作
db.executeSqlBEGIN事务,{}
.then res=>{
console.logres;
//在表上执行所有插入操作
//例如
//终于
db.executeSqlCOMMIT,{}
.then=>console.logCOMMIT
.catch=>console.loge;
}.catch=>console.loge;
//这个过程为我提供了更快的方法。上述问题对话框中的方法是在3分钟内在华硕Zenphone 1上插入4553个项目。此方法允许我在3分钟内插入10000个项目。
哪个是你的瓶颈?下载数据还是插入数据?4453元素不是那么多行。有很多数据吗?是的,我有两个大表,另外4个是小表。对于一个巨大的表,我必须下载的数据大约是该json数组中的12Megabytes4453个元素。在上面的示例中,我只展示了一个大表示例,因为大量代码可能会误导这一点。您应该使用apkSam打包一个预填充数据库,您能更具体一点吗如果安装时只需要大数据,那么可以在apk中包含静态文件。然后你就可以下载delta了。如果你需要定期刷新,那么它将无法工作。
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);
})