用于cordovaSQLite的批量插入 [英] Bulk Insert for cordovaSQLite

查看:106
本文介绍了用于cordovaSQLite的批量插入的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有用于cordovaSQLite的批量插入?
我有很多数据,我想保存在sqlite Db中。

Is there a Bulk insert for cordovaSQLite? I have a lot of Data, which I want to save in a sqlite Db.

我的代码现在是

var query = "INSERT INTO Card (CardId, CardSetId, FrontText, BackText, ControlType, CardLevel, IsDirty, ChangedAt, Active) VALUES (?,?,?,?,?,?,?,?,?)";

for (var i = 0; i < cardList.length; i++) {
    $cordovaSQLite.execute(db, query, [cardList[i].CardId, cardList[i].CardSetId, cardList[i].FrontText, cardList[i].BackText, cardList[i].ControlType, cardList[i].CardLevel, cardList[i].IsDirty, cardList[i].ChangedAt, cardList[i].Active]);
}

它有效,但非常慢!

当我使用此代码时:

var query = "INSERT INTO Card (CardId, CardSetId, FrontText, BackText, ControlType, CardLevel, IsDirty, ChangedAt, Active) VALUES ";
var data = [];
var rowArgs = [];
cardList.forEach(function (card) {
    rowArgs.push("(?,?,?,?,?,?,?,?,?)");
    data.push(card.CardId);
    data.push(card.CardSetId);
    console.log(card.CardSetId);
    data.push(card.FrontText);
    data.push(card.BackText);
    data.push(card.ControlType);
    data.push(card.CardLevel);
    data.push(card.IsDirty);
    data.push(card.ChangedAt);
    data.push(card.Active);
});
query += rowArgs.join(", ");
$cordovaSQLite.execute(db, query, [data]).then(function (res) {
    console.log("inserted");
    }, function(err) {
          console.dir(err);
});

然后我收到以下错误:sqlite3_step失败:NOT NULL约束失败:Card.CardSetId

Then I get the following error: sqlite3_step failure: NOT NULL constraint failed: Card.CardSetId

但是在数据数组中CardSetId不为空!

But in the data array CardSetId is not empty!

有没有办法让它更快,或者用它来做批量插入?

Is there a way to make it faster, or do it with a bulk insert?

谢谢

推荐答案

您可以尝试使用< a href =https://github.com/dpa99c/cordova-sqlite-porter =nofollow noreferrer> cordova-sqlite-porter 。使用 importJsonToDb()将您的插入作为JSON结构传递给它,它将< a href =https://github.com/dpa99c/cordova-sqlite-porter#json-import-optimisations =nofollow noreferrer>优化插入到SQLite DB中。

You can try using cordova-sqlite-porter. Pass it your inserts as a JSON structure using importJsonToDb() and it will optimise the insertion into the SQLite DB.

示例项目说明插入15,000多条记录。在三星Galaxy S4上,使用单个SQL插入语句执行此操作大约需要5分钟/ 300秒,但优化的JSON等效(使用UNION SELECT - 在这里查看信息)在同一台设备上大约需要3秒钟 - 速度提高100倍。

The example project illustrates insertion of 15,000+ records. On a Samsung Galaxy S4, performing this using single SQL insert statements takes around 5 minutes/300 seconds, but the optimised JSON equivalent (using UNION SELECT - see here for info) takes around 3 seconds on the same device - 100X faster.

这篇关于用于cordovaSQLite的批量插入的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆